MySQL time functions help manipulate, retrieve, compare, and format time-based values. They are essential for applications that track schedules, logs, durations, and real-time events.
Time functions in MySQL operate on TIME and DATETIME values. They return results in standard formats that can be further processed or formatted.
// Get current server time
SELECT CURTIME() AS current_time;
// Get current date and time
SELECT NOW() AS current_datetime;
// Extract time from datetime
SELECT TIME('2025-07-18 14:23:45') AS extracted_time;
// Add 2 hours to a time value
SELECT DATE_ADD('14:23:45', INTERVAL 2 HOUR) AS new_time;
// Subtract 30 minutes from a time
SELECT DATE_SUB('14:23:45', INTERVAL 30 MINUTE) AS new_time;
// Calculate difference between two times
SELECT TIMEDIFF('16:23:45','14:23:45') AS time_difference;
// Convert seconds into time format
SELECT SEC_TO_TIME(3600) AS time_format;
// Format time into HH:MM:SS
SELECT TIME_FORMAT('14:23:45','%H:%i:%s') AS formatted_time;
Each query returns a computed time value based on the function used. These outputs are commonly used in reports, dashboards, and scheduling logic.