TIMEDIFF
Returns the difference between two time or datetime values.
MySQL 8.1
TIMEDIFF(datetime1, datetime2)Parameters
- datetime1DATETIME
- Time or datetime to subtract datetime2 from
- datetime2DATETIME
- Value of the same type to subtract
Return value
A time (TIME), can be negative and longer than 24 hours, but not beyond 838:59:59; NULL if the arguments have different types
Examples
Difference within one day
MySQL 8.1
SELECT TIMEDIFF('2022-12-05 18:00:00', '2022-12-05 14:30:00') AS diff;Flight duration
MySQL 8.1
SELECT id,
town_from,
town_to,
TIMEDIFF(time_in, time_out) AS duration
FROM Trip;Negative difference
MySQL 8.1
SELECT TIMEDIFF('14:00:00', '18:00:00') AS diff;Too large a difference is capped
MySQL 8.1
SELECT TIMEDIFF('2022-12-05 18:00:00', '2021-12-01 14:00:00') AS diff;Details
To get the difference as a number of seconds, wrap the result in TIME_TO_SEC or use TIMESTAMPDIFF.