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

标签 php mysql timezone

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

根据我的服务器(相同时区,由 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/

相关文章:

php - MYSQL 单个查询既可以从一个表中检索单行,也可以从另一个表中检索多行作为单个字段

PHP 我的选择不起作用?

mysql - 同一张表在 2 个条件下内联一个表,有时任一条件都为假

php - 检查哪些用户在线

android - 在改造的 RequestBody 中转换 Date 的 TimeZone

php - 如何使用 Codeigniter 在 paypal 实时集成中启用信用卡/借记卡支付选项?

php - 是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用?

mysql - Laravel - 在现有表上添加外键数据

java - 在语言环境 Java 中显示时区名称

python - Pandas 中带有时区列的时间戳