mysqldude

TIMEDIFF

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
TIMEDIFF returns hours:minutes:seconds (original)
original post
✓✓✓✓✓✓✓✓✓
Split into days + time with FLOOR and MOD (original)
original post
✓✓✓✓✓✓✓✓✓
TIMEDIFF over ~35 days is capped at 838:59:59 (TIME range)
version check
⚠ 1292⚠ 1292⚠ 1292⚠ 1292⚠ 1292⚠ 1292⚠ 1292⚠ 1292⚠ 1292
TIMESTAMPDIFF(SECOND) has no 35-day limit (modern)
modern rewrite
✓✓✓✓✓✓✓✓✓
TIMEDIFF returns hours:minutes:seconds (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Split into days + time with FLOOR and MOD (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
TIMEDIFF over ~35 days is capped at 838:59:59 (TIME range) version check
MySQL 5.6⚠ 12925.7⚠ 12928.0⚠ 12928.4⚠ 12929⚠ 1292
MariaDB 10.6⚠ 129210.11⚠ 129211.4⚠ 129211.8⚠ 1292
TIMESTAMPDIFF(SECOND) has no 35-day limit (modern) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • TIMEDIFF over ~35 days is capped at 838:59:59 (TIME range) (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): warning 1292: Truncated incorrect time value: '1464:00:00'

Related errors: 1292

The original article

First published 2012-04-22. The text below is the original; the table above shows how it behaves on current versions.

Using the MySQL function timediff to get the time difference between two dates returns the number of hours, minutes and seconds between the two date times. In most cases this is sufficient, especially if the times are on the same date or within a day of each other. However, in some cases it may return a large number of hours from which we want to identify the number of days.

The MySQL time_format function does not provide a variable to return the number of days so this is something the needs to be calculated.

For example, let us imagine returning the number of hours since the 1st of April 2012 from today:

mysql> SELECT TIMEDIFF(now(), '2012-04-01 09:37:45');
+----------------------------------------+
| TIMEDIFF(now(), '2012-04-01 09:37:45') |
+----------------------------------------+
| 512:58:51                              |
+----------------------------------------+
1 row in set (0.00 sec)

While this is good the number 512 is large and difficult to determine the number of days. MySQL offers a few nice functions to help us out here:

TIME_TO_SEC
This function converts a time data type to an integer representing the number of seconds
SEC_TO_TIME
Converts the number of seconds back to a time data type
FLOOR
Returns a rounded down integer
x MOD y
Returns the remainder when x is divided by y

Calculation

Knowing that the number of seconds will allow us to calculate the number of days, we can use the above functions to help us:

The number of days is the number of seconds divided by 246060 rounded down. This is achieved by issuing FLOOR(seconds / (24 * 60 * 60)).

The number of minutes remaining is the remainder of the above operation. In other words seconds MOD (24 * 60 * 60).

mysql> SELECT Floor(Time_to_sec(Timediff(NOW(), '2012-04-01 09:37:45')) / (
    ->              60 * 60 * 24 )) AS
    ->        days,
    ->        Sec_to_time(Time_to_sec(Timediff(NOW(), '2012-04-01 09:37:45')) MOD (
    ->                    60 * 60 * 24 ))
    ->        AS timediff,
    ->        Timediff(NOW(), '2012-04-01 09:37:45');
+------+----------+----------------------------------------+
| days | timediff | Timediff(NOW(), '2012-04-01 09:37:45') |
+------+----------+----------------------------------------+
|   21 | 09:07:51 | 513:07:51                              |
+------+----------+----------------------------------------+
1 row in set