时间如何储存

发布于 2024-10-06 07:27:36 字数 250 浏览 0 评论 0原文

好吧,我有点困惑。我正在开发一个必须在数据库中存储一些 time() 的项目。在我看来,为了更好的灵活性,我应该将其存储为时间戳,以便我可以在 2 次和其他很酷的东西之间进行操作。 但我看到(这里比其他地方更多)很多人以日期格式将时间存储在数据库中,遇到一些常见问题,例如:如何将日期格式转换为时间戳?或者我如何将一个日期减去另一个具有不同日期格式的日期?等等。

我使用时间戳并稍后将其转换(显示给用户)为时间格式是否错误? Mysql数据库。

Ok, i'm a bit confused. I am working on a project that have to store some time() in the database. It seems to me that for better flexibility i should store it as timestamp so that i could make operation between 2 times and other cool stuff.
But i have seen (here more than everywhere else) that a lot of people store time in databases in date format, falling in some common questions like: how can i convert date format in timestamp? or how can i subtract a date to another that have different date format? etc.

Am i wrong using timestamps and convert it later (to be shown to the user) to time format?
Mysql database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-10-13 07:27:36

MySql 支持 DateTime、Date 和 Timestamp 列 并提供 许多方便的函数通过SQL。我建议阅读链接的手册页,选择适合您需求的手册页。

MySql supports DateTime, Date and Timestamp columns and offers a number of convenient functions to work with them through SQL. I suggest to read through the linked manual page to pick the right one for your needs.

童话 2024-10-13 07:27:36

我倾向于以 int(10) 格式存储时间戳,因为 UNIX 时间戳始终为 10 个字符长。建议使用 int 而不是 varchar 或 char,因为当您使用 SQL 的 ORDER 语法时,它不会得到错误的顺序。

假设您想从日期转换为时间戳,您可以使用 strtotimemktime,具体取决于您在做什么。

要计算两个时间戳之间的差异,只需将它们相减即可。

对于时区问题,您可以使用 date_default_timezone_set 将其设置为用户所在的国家/地区。当您使用 date 等函数时,将会考虑到这一点

I tend to store timestamps in int(10) format, as a UNIX timestamp will always be 10 characters long. It's recommended to use int instead of varchar or char as when you use SQL's ORDER syntax it won't get the order incorrect.

Say you want to convert from a date to a timestamp, you could either use strtotime or mktime, depending on what you're doing.

To work out the difference between two timestamps just subtract them from one another.

For the issue of timezones you can set this to the user's country using date_default_timezone_set. This will be taken into account when you're using functions like date

乖乖兔^ω^ 2024-10-13 07:27:36

以防万一您不知道,MySQL 支持多种日期函数,DATESUB、DATEADD 等。

Just in case you didn't know, MySQL supports several date functions, DATESUB, DATEADD etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文