TIMESTAMPDIFF
PostgreSQL equivalent:
Returns the difference between two dates in the specified units.
MySQL 8.1
TIMESTAMPDIFF(unit, datetime1, datetime2)Parameters
- unitkeyword
- Unit to measure the difference in
- Values:MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEAR
- datetime1DATETIME
- Start date, subtracted from datetime2
- datetime2DATETIME
- End date
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.1
SELECT TIMESTAMPDIFF(YEAR, '2012-06-12', '2022-06-11') AS years;Age on a given date
MySQL 8.1
SELECT member_name,
TIMESTAMPDIFF(YEAR, birthday, '2024-01-01') AS age
FROM FamilyMembers;Flight duration in minutes
MySQL 8.1
SELECT id,
town_from,
town_to,
TIMESTAMPDIFF(MINUTE, time_out, time_in) AS minutes
FROM Trip;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.