MySQL DATEDIFF vs TIMESTAMPDIFF Guide

The datediff function calculates the difference in days between two dates. The timestampdiff function calculates the difference in time between two dates, allowing for different time units (such as days, hours, minutes, etc.) to be selected for calculation.

An example using the datediff function:

SELECT DATEDIFF('2021-06-01', '2021-05-01');

The example above calculates the difference in days between May 1, 2021 and June 1, 2021, with a result of 31 days.

Example using the timestampdiff function:

SELECT TIMESTAMPDIFF(DAY, '2021-05-01 12:00:00', '2021-06-01 14:30:00');

The above example calculates the difference in days between May 1, 2021 at 12:00:00 and June 1, 2021 at 14:30:00, resulting in 31 days.

Please note that for the timestampdiff function, the first parameter represents the unit of time, which can be chosen as DAY, HOUR, MINUTE, and so on. The second and third parameters are datetime values.

bannerAds