Storing datetime as UTC in PHP/MySQL

前端 未结 6 1624
悲哀的现实
悲哀的现实 2020-11-30 22:08

Everywhere I read about converting time to a user\'s timezone says that the best method is to store a date and time in UTC then just add the user\'s timezone offset to this

6条回答
  •  抹茶落季
    2020-11-30 22:24

    MySQL: UTC_TIMESTAMP()

    Returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context

    PHP: gmdate()

    Also PHP date_default_timezone_set() is used in PHP to set the current time zone for the script. You can set it to the client time zone so all the formatting functions return the time in his local time.

    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.

提交回复
热议问题