在数据库中存储时间(hh:mm)的最佳方式 [英] Best way to store time (hh:mm) in a database

查看:1310
本文介绍了在数据库中存储时间(hh:mm)的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据库表中存储时间,但只需要存储小时和分钟。
我知道我可以使用DATETIME并忽略日期的其他组件,但是没有存储比我实际需要的更多的信息,最好的方法是什么?

解决方案

您可以将其存储为午夜过去的分钟数的整数:



  0 = 00:00 
60 = 01:00
252 = 04:12

然而,你需要编写一些代码来重构时间,但这不应该是棘手的。 >

I want to store times in a database table but only need to store the hours and minutes. I know I could just use DATETIME and ignore the other components of the date, but what's the best way to do this without storing more info than I actually need?

解决方案

You could store it as an integer of the number of minutes past midnight:

eg.

0 = 00:00 
60 = 01:00
252 = 04:12

You would however need to write some code to reconstitute the time, but that shouldn't be tricky.

这篇关于在数据库中存储时间(hh:mm)的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆