Storing timespans in a MySQL database
Leonel Elimpe
by Leonel Elimpe
1 min read


  • MySQL

I’ve been working on coupon/promotion codes functionality and one requirement is only allowing the re-use of a coupon code after a period of time has passed. It could be a couple of hours, a couple of days, or a month at most.

Now how to persist this period of time a.k.a timespan to the database? After some time Googling I came across this Stackoverflow answer which presents the best solution for my use case:

A MySQL TIME type can store time spans from ‘-838:59:59’ to ‘838:59:59’ - if that’s within your required range, and it would be useful to have MySQL natively present these spans in HH:MM:SS form, then use that.

The time range supported by the TIME type is well within what I need, which is 30 days which equals 720 hours.

I equally considered the following solutions before going with the above:

Regarding a user interface for inputting this time value, you can have a look at: