MySQL TIME_TO_SEC() Function

The TIME_TO_SEC() function in MySQL converts a time value expressed in hours, minutes, and seconds into its equivalent total number of seconds.



TIME_TO_SEC(): Definition and Usage

TIME_TO_SEC() is useful when you need to work with time durations as a single numerical value (in seconds). This is common in calculations or comparisons involving time intervals.

Syntax

Syntax

TIME_TO_SEC(time)
      

Parameter Values

Parameter Description
time The time value to convert (expressed in HH:MM:SS format, or HH:MM:SS.mmmmmm). This is required.

Examples

Converting a Time Value to Seconds

This converts the time "19:30:10" (19 hours, 30 minutes, 10 seconds) into seconds.

Syntax

SELECT TIME_TO_SEC("19:30:10");
      
Output

70010
      

Converting Other Time Values

These examples show conversions with different time values. Note that microseconds are included in the total seconds.

Syntax

SELECT TIME_TO_SEC("00:00:05"); --5 seconds
SELECT TIME_TO_SEC("03:30:00.999999"); --3 hours, 30 minutes, and almost 1 second
SELECT TIME_TO_SEC("-03:30:00"); --Negative time value.
      
Output

5
12601
-12600