简体   繁体   中英

MySQL TIME vs DECIMAL

I want to store in DB START time and END time . Important are only hours and minutes , so in form user have 4 dropdowns (start_time_hours, start_time_minutes, end_time_hours, end_time_minutes). Values for hours - [00-23], for minutes - [00, 15, 30, 45]. At the end I would like to have substraction from both in decimal.

I'm wondering is it better to store 2 TIME fields (for start and end time) or 2 DECIMAL fields?

In this case minutes can be smoothly converted to decimal: [00 => 00, 15 => 0.25, 30 => 0.5, 45 => 0.75], so time 19:15 would turn out as 19.25. I guess DB storage-wise it's the same, - as TIME and DECIMAL(4,2) both takes up 3 bytes, if I'm not wrong :)

So, for example, after saving data to DB, I go back to edit the form and expect the saved values to be in those 4 dropdowns. If DB data would be TIME type, then I should use something like

$hours = date('G', strtotime($start_time)) && $minutes = date('i', strtotime($start_time))

for START time dropdowns, but if data would be DECIMAL, then

$hours = floor($start_time); && $minutes = $start_time - $hours;

Which of these 2 ways would be faster?

It could be some other kind of calculations between these 2 times, so I guess for all of those for TIME type data it would involve date() or(and) strtotime() functions, but for DECIMAL type only mathematical functions? So, which solution would be best for speed and DB? Maybe there is even better solution for this case?

I guess one disadvantage for DECIMAL is scalabilty in the future, for exapmle if we decide to let user choose minutes from 1-60, - then we can't get precise DECIMAL for 18:10, as it would be 18.16666...

I recommend the TIME type. The time type is more meaningful. Maybe decimal type is little faster than TIME type sometimes. But when you using decimal, you should ensure that everyone calculate rightly on everywhere, not just on your code. I think that is more important than size and speed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM