Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the best way to store a time duration in a MySQL larger than the TIME range?

I'm in need of a method to store a time duration in a db field. I'm building a website where customers should be able to choose how long they would like an advert to display from a particular start date.

I had thought about using TIME but that has a max of '838:59:59' which works out at about 34 days. Its possible that a client would want an advert to exist for longer than that.

So what would be the best way to deal with this? Just a really large INT?

like image 602
cosmicsafari Avatar asked Nov 12 '12 15:11

cosmicsafari


People also ask

How to store time duration in MySQL?

For the time interval, you can use the 'D HH:MM:SS' format where D represents days with a range from 0 to 34. A more flexible syntax is 'HH:MM' , 'D HH:MM' , 'D HH' , or 'SS' . If you use the delimiter:, you can use 1 digit to represent hours, minutes, or seconds. For example, 9:5:0 can be used instead of '09:05:00' .

What should be the datatype of duration in SQL?

The duration type holds relative datetime values. This type provides application developers the ability to store values like “2 minutes” or “3 years.” In other words, values that should not be interpreted as absolute dates or times.

How to write time in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


2 Answers

If you intend to have a column for start time and one for duration, I think you can store it in seconds. So, I assume you will have something like this;

+-----------+--------------------------+------------------+
| advert_id | start_time               | duration_seconds |
+-----------+--------------------------+------------------+
| 2342342   |'2012-11-12 10:23:03'     | 86400            |
+-----------+--------------------------+------------------+

(For the sake of the example, we will call this table adverts)

  1. advert_id - a key pointing to your advert
  2. start_time - the time the advert should start (data type - TIMESTAMP)
  3. duration_seconds - Time in seconds that the advert is supposed to "live" (INTEGER(11)

    SELECT TIME_TO_SEC(timediff(now(),start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts;

If you want to get only adverts that have not expired, you will run a query like this;

SELECT * FROM  `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;

That's one way I would do it if I were to go with the "duration" field.

like image 86
mwangi Avatar answered Oct 16 '22 16:10

mwangi


Yes, you can store time as INT data type (or another big integer: MEDIUMINT, LONGINT). Then use you can easily get days and time part from this, e.g. -

SELECT time DIV 86400 AS days, SEC_TO_TIME(column1 MOD 86400) AS time FROM table

Where 86400 is a number of seconds in 24h (60 * 60 * 24 = 86400).

like image 21
Devart Avatar answered Oct 16 '22 16:10

Devart