EN
MySQL - TIMEDIFF() function example
0
points
In this article, we would like to show you TIMEDIFF()
function example in MySQL.
Quick solution:
-- SELECT TIMEDIFF('to_time','from_time');
SELECT TIMEDIFF('HH:MM:SS','HH:MM:SS');
or
-- SELECT TIMEDIFF('to_time','from_time');
SELECT TIMEDIFF('YYYY-MM-DD HH:MM:SS','YYYY-MM-DD HH:MM:SS');
Where:
YYYY
- four-digit year format, e.g.2021
,MM
- month number (counted from01
to12
),DD
- day number (from01
to31
- depending on month),HH
- hour number (from00
to24
),MM
- minute number (from00
to59
),SS
- second number (from00
to59
).
TIMEDIFF()
function returns the difference between two time or datetime expressions.
Practical example
Example 1 - with time
In this example, we will calculate the difference between two times using TIMEDIFF()
function.
Query:
SELECT TIMEDIFF('09:30:00','08:00:00') AS 'time difference';
Result:
Example 2 - with datetime
In this example, we will calculate the difference between two datetimes using TIMEDIFF()
function.
Query:
SELECT TIMEDIFF('2021-01-09 09:00:00','2021-01-08 08:00:00') AS 'time difference';
Result: