TIMESTAMPDIFF() in MySQL — How It Works
What the MySQL TIMESTAMPDIFF() function does, with an example.
Published October 20, 2026
TIMESTAMPDIFF(unit, start, end) returns the difference between two datetimes in the chosen unit (SECOND, MINUTE, HOUR, DAY, MONTH, YEAR).
How it works
The result is an integer, truncated, and positive when end is after start.
Try it in MySQL
SELECT TIMESTAMPDIFF(HOUR, '2026-11-12 08:00', '2026-11-12 14:30')
6
SELECT TIMESTAMPDIFF(YEAR, '1990-05-01', CURDATE())
36
SELECT TIMESTAMPDIFF(DAY, created_at, NOW()) FROM users LIMIT 1
42
Practical examples
Example 1
SELECT TIMESTAMPDIFF(HOUR, '2026-11-12 08:00', '2026-11-12 14:30');
-- 6Example 2
SELECT TIMESTAMPDIFF(YEAR, '1990-05-01', CURDATE());
-- 36Example 3
SELECT TIMESTAMPDIFF(DAY, created_at, NOW()) FROM users LIMIT 1;
-- 42Use it in a real query
SELECT id, TIMESTAMPDIFF(HOUR, '2026-11-12 08:00', '2026-11-12 14:30') AS result
FROM your_table
LIMIT 10;Common mistakes
- Swapping start and end.
- Expecting fractional results.
- Calculating ages with DATEDIFF/365.
Key takeaways
- TIMESTAMPDIFF() in one line: TIMESTAMPDIFF(unit, start, end) returns the difference between two datetimes in the chosen unit (SECOND, MINUTE, HOUR, DAY, MONTH, YEAR).
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
Age in years?
TIMESTAMPDIFF(YEAR, birth_date, CURDATE()).
Does TIMESTAMPDIFF() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.