In this article, we would like to show you how to calculate the difference between two dates in MySQL.
-- SELECT DATEDIFF('to_date','from_date') SELECT DATEDIFF('YYYY-MM-DD','YYYY-MM-DD');
or (with time):
-- SELECT DATEDIFF('to_date','from_date') SELECT DATEDIFF('YYYY-MM-DD HH:MM:SS','YYYY-MM-DD HH:MM:SS');
YYYY- four-digit year format, e.g.
MM- month number (counted from
DD- day number (from
31- depending on month),
HH- hour number (from
MM- minute number (from
SS- second number (from
In this example, we will calculate the number of days from
SELECT DATEDIFF('2021-01-08','2021-01-05') AS 'difference (in days)';
SELECT DATEDIFF('2021-01-08 09:35:27','2021-01-05 11:22:55') AS 'difference (in days)';
Both of the above queries give the same result (difference between
3days). They don't take time into account.