MySQL TIMEDIFF Function

MySQL TIMEDIFF function is one of the MySQL Date Functions, used to find the difference between two time expressions and returns the time value. Let me show you how to use this MySQL Time diff function to find the difference between different time values with an example. The basic syntax of the TIMEDIFF Function is as shown below:

TIMEDIFF(Expression1, Expression2);

This Time difference function subtracts Expression2 from Expression1 (Exp1 – Exp2) and returns the time value. The return value of this Date Function is limited to the range allowed by the TIME() function.

MySQL TIMEDIFF function Example

The below shown queries help you understand the use of this MySQL Time Difference function. Here, we are finding the difference between different time expressions.

SELECT TIMEDIFF('10:12:22', '02:05:11');

SELECT TIMEDIFF('14:12:22', '23:05:11');

SELECT TIMEDIFF('14:12:59.999888', '03:59:11.111222');
MySQL TIMEDIFF Function 1

This time, we are using the MySQL DateTime expressions as an argument values. Next, we are finding the time difference between them.

SELECT TIMEDIFF('2019-02-28 10:12:22', '2019-01-31 02:05:11');

SELECT TIMEDIFF('2019-02-28 10:12:22', '2018-12-31 02:05:11');

SELECT TIMEDIFF('2019-02-05 10:12:22', '2019-02-28 02:05:11');
MySQL TIMEDIFF Function 2

In this MySQL timediff example, we are finding the Time difference between the current date and time returned by the LOCALTIME, Now(), and UTC_TIMESTAMP functions.

SELECT TIMEDIFF(LOCALTIME(), NOW());

SELECT TIMEDIFF(UTC_TIMESTAMP(), NOW());

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP());
MySQL TIMEDIFF Function 3