我目前正在研究在我的网站上处理时区的最佳方式。来自许多不同国家的人会同时访问它,我必须向他们展示基于时间的信息,所以我想:
根据我的服务器(相同时区,由 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()
orCURTIME()
, and values stored in and retrieved fromTIMESTAMP
columns. Values forTIMESTAMP
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/