MySQL TIMEDIFF() Function
In MySQL, the TIMEDIFF() function returns the difference between two time values.
TIMEDIFF() Syntax
Here is the syntax of MySQL TIMEDIFF() function:
TIMEDIFF(time1, time2)
Parameters
time1- Required. A time or datetime expression.
time2- Required. Another time or datetime expression.
Return value
The MySQL TIMEDIFF() function function returns the difference between two time values, it returns a time value in HH:MM:SS format.
If the specified expression is not a valid time or datetime, the TIMEDIFF() function will return NULL.
If the argument is NULL, the TIMEDIFF() function will return NULL.
TIMEDIFF() Examples
Here are some examples of the TIMEDIFF() function.
Difference between two time values
SELECT
TIMEDIFF('12:12:12', '12:10:10'),
TIMEDIFF('12:12:12', '10:10:10');
+----------------------------------+----------------------------------+
| TIMEDIFF('12:12:12', '12:10:10') | TIMEDIFF('12:12:12', '10:10:10') |
+----------------------------------+----------------------------------+
| 00:02:02 | 02:02:02 |
+----------------------------------+----------------------------------+Difference between two datetime values
SELECT TIMEDIFF('2022-02-28 12:12:12', '2022-02-21 10:10:10');
+--------------------------------------------------------+
| TIMEDIFF('2022-02-28 12:12:12', '2022-02-21 10:10:10') |
+--------------------------------------------------------+
| 170:02:02 |
+--------------------------------------------------------+Difference between a time and now
SELECT TIMEDIFF(NOW(), '2022-02-28 10:10:10');
+----------------------------------------+
| TIMEDIFF(NOW(), '2022-02-28 10:10:10') |
+----------------------------------------+
| 838:59:59 |
+----------------------------------------+Here, we use the NOW() function to get the current moment. You can also use CURDATE(), CURRENT_DATE(), SYSDATE().