MySQL TIMEDIFF function is one of the Date methods that is useful to find the difference between two time expressions and returns the output. Let me show you how to use this MySQL timediff function to find the difference between different time values with an example, and the basic syntax is as shown below:
This function subtracts Expression2 from Expression1 (Exp1 – Exp2) and returns the time value. The return value of this Date method 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 Time Difference function. Here, we are finding the difference between different 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');
In this example, we are using the MySQL DateTime expressions as argument values. Next, we find 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');
In this timediff example, we are finding the Time difference between the current datetime returned by the LOCALTIME, Now(), and UTC_TIMESTAMP methods.
SELECT TIMEDIFF(LOCALTIME(), NOW()); SELECT TIMEDIFF(UTC_TIMESTAMP(), NOW()); SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP());