MySQL Function

TIMEDIFF – MySQL Function

TIMEDIFF – MySQL Function

This returns the difference between two datetime expressions:

For example:

SELECT TIMEDIFF(“2020-05-10 13:10:11”, “2020-05-05 13:10:10”);

Output:

120:00:01

Let’s Understand the few things about timediff function

  • The TIMEDIFF() function returns the difference between two time/datetime expressions.
  • time1 and time2 should be in the same format, and the calculation is time1 – time2.

Let see few cases to understand better timediff function:

mysql> SELECT TIMEDIFF(’12:00:00′,’10:00:00′) diff;
+———-+
| diff |
+———-+
| 02:00:00 |
+———-+
1 row in set (0.00 sec)
mysql> SELECT
TIMEDIFF(‘2010-01-01 01:00:00’,
‘2010-01-02 01:00:00’) diff;
+———–+
| diff |
+———–+
| -24:00:00 |
+———–+
1 row in set (0.00 sec)
mysql> SELECT TIMEDIFF(‘2010-01-01’,NULL) diff;
+——+
| diff |
+——+
| NULL |
+——+
1 row in set, 1 warning (0.00 sec)

mysql> SELECT TIMEDIFF(
‘2010-01-01 10:00:00′,
’10:00:00’) diff;
+——+
| diff |
+——+
| NULL |
+——+
1 row in set (0.00 sec)