Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store duration in MySQL

Tags:

php

mysql

I need to store duration (number of minutes and/or hours and/or days). I have looked at MySQL type, but DATE and TIME types seems to be used to store a specific date in time. Not a duration.

I thinking about using a int to store the number of second, and convert it in the PHP for display.

like image 441
user457015 Avatar asked Dec 04 '10 17:12

user457015


People also ask

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 does MySQL store time?

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 datatype is used for duration?

The xs:duration data type represents a duration of time that is expressed by the Gregorian year, month, day, hour, minute, and second components. Derived from data type xdt:anyAtomicType.

What is length values in MySQL?

The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.


2 Answers

You seem to have the answer yourself. You can store the number of milliseconds(or any unit) in the duration as a number and convert it to the unit that you wish in the application.

like image 117
Vincent Ramdhanie Avatar answered Sep 27 '22 17:09

Vincent Ramdhanie


You can store the value as a number.

like image 38
user685515 Avatar answered Sep 27 '22 19:09

user685515