Function Reference

TIME_TO_SEC

PostgreSQL equivalent:

Converts a time to a number of seconds.

MySQL 8.1
TIME_TO_SEC(time)

Parameters

timeTIME
Time or datetime

Return value

An integer number of seconds, negative for a negative time; for a datetime only the time is used

Examples

Time to seconds

MySQL 8.1
SELECT TIME_TO_SEC('01:02:03');
TIME_TO_SEC('01:02:03')
3723

Flight duration in seconds

MySQL 8.1
SELECT id,
	TIMEDIFF(time_in, time_out) AS duration,
	TIME_TO_SEC(TIMEDIFF(time_in, time_out)) AS duration_sec
FROM Trip;
iddurationduration_sec
110003:20:0012000
110103:33:0012780
112311:20:0040800
112410:50:0039000
114501:48:006480
114602:06:007560
118101:49:006540
118201:55:006900
118701:57:007020
118801:58:007080

Details

For a datetime, only the time of day is used: TIME_TO_SEC('2022-06-16 01:00:00') returns 3600. To get the number of seconds between two datetimes, use TIMESTAMPDIFF(SECOND, start, end).

See also