mysqldude

Sum time ignoring seconds

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SUM of seconds vs SUM after DIV 60 / FLOOR (original)
original post
✓✓✓✓✓✓✓✓✓
SUM(diff) on a TIME column adds hhmmss as numbers: 76010, not 08:01:30
version check
✓✓✓✓✓✓✓✓✓
SUM of seconds vs SUM after DIV 60 / FLOOR (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
SUM(diff) on a TIME column adds hhmmss as numbers: 76010, not 08:01:30 version check
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

The original article

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

I have seen this requested many times especially for reporting. Let’s imagine we need to generate a report which displays hours and minutes and a sum of the reported times. Imagine we have the following information in our database:

mysql> select starttime, endtime, diff, time_format(diff, '%H:%i') reportedtime from timer;
+-----------+----------+----------+--------------+
| starttime | endtime  | diff     | reportedtime |
+-----------+----------+----------+--------------+
| 09:09:53  | 17:05:23 | 07:55:30 | 07:55        |
| 09:09:00  | 09:09:01 | 00:00:01 | 00:00        |
| 09:09:00  | 09:09:30 | 00:00:30 | 00:00        |
| 09:09:00  | 09:09:59 | 00:00:59 | 00:00        |
| 09:00:00  | 09:00:30 | 00:00:30 | 00:00        |
| 09:00:00  | 09:01:59 | 00:01:59 | 00:01        |
| 09:00:00  | 09:01:00 | 00:01:00 | 00:01        |
| 09:00:00  | 09:01:01 | 00:01:01 | 00:01        |
+-----------+----------+----------+--------------+
8 rows in set (0.01 sec)

So far so good. By what if we perform a sum on the field “diff”.

mysql> select sec_to_time(sum(time_to_sec(diff))) from timer;
+-------------------------------------+
| sec_to_time(sum(time_to_sec(diff))) |
+-------------------------------------+
| 08:01:30                            |
+-------------------------------------+
1 row in set (0.00 sec)

While this is accurate it does not match the reported hours and minutes:

mysql> select time_format(diff, '%H:%i') reportedtime from timer;
+--------------+
| reportedtime |
+--------------+
| 07:55        |
| 00:00        |
| 00:00        |
| 00:00        |
| 00:00        |
| 00:01        |
| 00:01        |
| 00:01        |
+--------------+
8 rows in set (0.00 sec)

As a result we need to round the content to the nearest minute before performing the calculation. In order to round there are several ways to perform this:

  • Using FLOOR(time / 60) * 60 which rounds the content down returning an integer
  • Using the (time DIV 60) * 60 operator which performs a division returning an integer

Here is an example of both operations:

mysql> SELECT Sec_to_time(Sum(Time_to_sec(diff)))                  accurate,
    ->        Sec_to_time(Sum(Time_to_sec(diff) DIV 60) * 60)      roundeddiv,
    ->        Sec_to_time(Sum(Floor(Time_to_sec(diff) / 60)) * 60) roundedfloor
    -> FROM   timer;
+----------+------------+--------------+
| accurate | roundeddiv | roundedfloor |
+----------+------------+--------------+
| 08:01:30 | 07:58:00   | 07:58:00     |
+----------+------------+--------------+
1 row in set (0.01 sec)