MySQL TIMEDIFF Function Explained
The TIMEDIFF() function in MySQL is used to calculate the difference between two times and returns a time interval.
The syntax of the TIMEDIFF() function is as follows:
Calculate the difference in time between time1 and time2.
Here, time1 and time2 are parameters that represent time and can be in the form of a time string, a timestamp, or a value of the TIME type.
The TIMEDIFF() function returns a time interval represented as a string in the format “HH:MM:SS”, where HH stands for hours, MM stands for minutes, and SS stands for seconds.
Here are a few examples:
The result of SELECT TIMEDIFF(’12:00:00′, ’09:00:00′) is ’03:00:00′, indicating a 3-hour difference between the two times.
The result of the query SELECT TIMEDIFF(‘2022-01-01 12:00:00’, ‘2022-01-01 09:00:00′) is ’03:00:00’, indicating a 3-hour difference between the two times.
The result of the query is ’03:00:00′, indicating a 3-hour difference between the two times.
It is important to note that the TIMEDIFF() function calculates the difference in time, not in dates. If time1 is less than time2, the returned time interval will be a negative value.