在数据库中将日期和时间存储为纪元与本机日期时间格式
对于我的大多数任务,我发现使用纪元格式的日期和时间要容易得多:
这个列表可以继续下去,但对我来说,为了保持代码的可移植性,这足以放弃数据库的本机日期时间格式并将日期和时间存储为整数。你们觉得怎么样?
For most of my tasks I find it much easier to work with date and time in the epoch format:
This list can go on, but for me in order to keep my code portable that's enough to ditch database's native datetime format and store date and time as integer. What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过使用 UTC 日期时间来解决时区问题。我知道 C 有一个将纪元时间转换为 UTC 的函数。我找不到一个可以走另一条路的人,这让我很困惑。
You should be able to solve the time zone issue by using UTC datetimes. I know C has a function for converting epoch times to UTC. I couldn't find one to go the other way, which confuses me.
我认为我非常习惯 MVC 中正确的模型对象和方法,这些日期和时间功能变得微不足道。我取决于日期和时间时间库进行计算。很少有时候我必须自己编写,实际上那些时候我忽略了这个功能已经在库中的事实。如果您使用 PHP,请从 Zend Framework 获取日期和时间库。一旦开发了模型,就可以重复使用它。那么您就不必关心日期和时间如何存储在数据库中。
I think I'm so use to proper Model objects and methods in MVC these dates and times functionality becomes trivial. I depend on the date & time library to the calculations. There's very few times where I have to write my own and and actually those times I overlooked the fact that the functionality was in the library already. If you are using PHP grab the date and time library from the Zend Framework. Once you have developed your model reuse it. Then you don't care how the date and time is stored in the DB.
使用数据库的
DATETIME
类型的主要原因是 POSIXtime_t
的范围非常有限;你不能存储超过臭名昭著的 Y2038 或 1904 年之前的日期。由于许多数据库需要存储历史或未来日期,因此有一个完整的范围且通常可移植的DATETIME
;如果事实上 POSIX 纪元时间对您更有用,那么请继续使用它们。The primary reason for using the database's
DATETIME
type is that the POSIXtime_t
has a very limited range; you cant store dates beyond the infamous Y2038, or before something around 1904. Since many databases need to store historical or future dates, there is a full range and usually portableDATETIME
; if in fact POSIX epoch times are more useful to you then go ahead and use them.