MariaDB TIMEDIFF() Function

In MariaDB, TIMEDIFF() is a built-in function that returns the difference between two time or datetime values.

MariaDB TIMEDIFF() Syntax

This is the syntax of the MariaDB TIMEDIFF() function:

TIMEDIFF(time1, time2)

Parameters

time1

Required. A time or datetime expression.

time2

Required. Another time or datetime expression.

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TIMEDIFF'.

Return value

The MariaDB TIMEDIFF() function returns the difference between two time values ​​or datetime values and the result time value is in the 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.

MariaDB TIMEDIFF() Examples

Example 1 - time value

SELECT TIMEDIFF('12:12:12', '12:10:10');

Output:

+----------------------------------+
| TIMEDIFF('12:12:12', '12:10:10') |
+----------------------------------+
| 00:02:02                         |
+----------------------------------+

Example 2 - datetime value

SELECT TIMEDIFF('2023-01-28 12:12:12', '2023-01-28 10:11:12');

Output:

+--------------------------------------------------------+
| TIMEDIFF('2023-01-28 12:12:12', '2023-01-28 10:11:12') |
+--------------------------------------------------------+
| 02:01:00                                               |
+--------------------------------------------------------+

Example 3 - Current datetime

SELECT TIMEDIFF(NOW(), '2023-01-01 10:10:10');

Output:

+----------------------------------------+
| TIMEDIFF(NOW(), '2023-01-01 10:10:10') |
+----------------------------------------+
| 267:29:26                              |
+----------------------------------------+

Here, we use the NOW() function to get the current moment. In addition to this, you can also use CURDATE(), CURRENT_DATE(), or SYSDATE().

Conclusion

In MariaDB, TIMEDIFF() is a built-in function that returns the difference between two time or datetime values.