我读到的关于将时间转换为用户时区的所有文章都说,最好的方法是将日期和时间存储为UTC,然后将用户的时区偏移量添加到这个时间中.
如何以UTC时间存储日期?我使用MySQL日期时间字段.
在PHP代码中向MySQL添加新记录时,我会使用now()
插入MySQL DATETIME.
我需要使用不同于now()
的东西来存储UTC时间吗?
我读到的关于将时间转换为用户时区的所有文章都说,最好的方法是将日期和时间存储为UTC,然后将用户的时区偏移量添加到这个时间中.
如何以UTC时间存储日期?我使用MySQL日期时间字段.
在PHP代码中向MySQL添加新记录时,我会使用now()
插入MySQL DATETIME.
我需要使用不同于now()
的东西来存储UTC时间吗?
MySQL:UTC_TIMESTAMP()
返回当前UTC日期和时间
菲律宾比索:gmdate()
PHP中还使用PHPdate_default_timezone_set()
设置脚本的当前时区.您可以将其设置为客户端时区,以便所有格式化函数返回本地时间中的时间.
In truth though I had a hard time getting this to work and always stumble into some gotcha. Eg. time information returned from MySQL is not formatted as 'UTC' so strtotime transforms it into a local time if you are not careful. I'm curious to hear if someone has a reliable solution for this problem, one that doesn't break when dates traverse media boundaries (HTTP->PHP->MySQL and MySQL->PHP->HTTP), also considering XML and RSS/Atom.