gpt4 book ai didi

php - 在 PHP 和 MySQL 上处理时区的最佳方法是什么?

转载 作者:IT王子 更新时间:2023-10-28 23:43:57 27 4
gpt4 key购买 nike

我目前正在研究在我的网站上处理时区的最佳方式。来自许多不同国家的人会同时访问它,我必须向他们展示基于时间的信息,所以我想:

根据我的服务器(相同时区,由 PHP 定义)每次都存储在数据库中然后,用户可以选择他的时区,我使用 mysql 函数 DATEADD 进行所需的转换。

这似乎工作正常,但我的问题是:

  • 这是最好的方法吗?
  • DATEADD 是处理时差最有效的函数吗?

谢谢。

最佳答案

MySQL Server Time Zone Support 中所述:

The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.

因此,如果您使用TIMESTAMP 类型的列,MySQL 将自动为您处理时区转换:只需在其time_zone 中为 session 设置适当的时区即可。变量。

关于php - 在 PHP 和 MySQL 上处理时区的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com