Function Reference

TIMESTAMPDIFF

PostgreSQL equivalent:

Returns the difference between two dates in the specified units.

MySQL 8.4
TIMESTAMPDIFF(unit, datetime1, datetime2)
unit
Unit to measure the difference in
Keyword
MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEAR
datetime1
Start date, subtracted from datetime2
DATETIME
datetime2
End date
DATETIME

Return value

An integer number of whole units (datetime2 minus datetime1), negative if datetime2 is earlier; an incomplete year or month is not counted

Examples

Incomplete year not counted

MySQL 8.4
SELECT TIMESTAMPDIFF(YEAR, '2012-06-12', '2022-06-11') AS years;
years
9

Age on a given date

MySQL 8.4
SELECT member_name,
	TIMESTAMPDIFF(YEAR, birthday, '2024-01-01') AS age
FROM FamilyMembers;
member_nameage
Headley Quincey63
Flavia Quincey60
Andie Quincey40
Lela Quincey38
Annie Quincey35
Ernest Forrest62
Constance Forrest55
Wednesday Addams18

Flight duration in minutes

MySQL 8.4
SELECT id,
	town_from,
	town_to,
	TIMESTAMPDIFF(MINUTE, time_out, time_in) AS minutes
FROM Trip;
idtown_fromtown_tominutes
1100RostovParis200
1101ParisRostov213
1123RostovVladivostok680
1124VladivostokRostov650
1145MoscowRostov108
1146RostovMoscow126
1181RostovMoscow109
1182MoscowRostov115
1187RostovMoscow117
1188MoscowRostov118

Details

Unlike DATEDIFF, the first argument is subtracted from the second: both TIMESTAMPDIFF(DAY, '2022-12-01', '2022-12-05') and DATEDIFF('2022-12-05', '2022-12-01') return 4.

See also

Where to learn