Storing time with milliseconds in database

Personally, I'd use integer types for duration

Example: 340,000 milliseconds is 340000 in an unsigned int column.

To me, datetime, time, date etc are for explicit points in time. 340 seconds is meaningless in that context. Having a start date/time allows the duration to be added of course


If you use MySQL 5.6.4 or later, you can use the TIME(3), DATETIME(3) and TIMESTAMP(3) column types to store up to 6 fractional digits (replace 3 with the number of fractional digits you need). Fractional part is added to time string after dot; example: 2018-09-08 17:51:04.781.

Official reference


It depends on how long you want to measure. If you won't be recording any times longer than 2^32, or 4294967296 ms than a regular unsigned int is just fine. That corresponds to about 50 days of clock time, btw.

If you were storing unix time stamps or larger intervals then look at using bigint. This gives you 8 bytes of space, or the ability to record times as long at 1.84467440737096e+19 ms (which is roughly 500 Million years)