Storing datetime as UTC in PHP/MySQL

前端 未结 6 1623
悲哀的现实
悲哀的现实 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:27

    I would suggest inserting the date in UTC time zone. This will save you a lot of headaches in the future with daylight saving problems.

    INSERT INTO abc_table (registrationtime) VALUES (UTC_TIMESTAMP())
    

    When I query my data I use the following PHP script:

    setTimezone(new DateTimeZone('Europe/Istanbul'));
      echo $formatted_date_long=date_format($dt_obj, 'Y-m-d H:i:s');
    }
    ?>
    

    You can replace the DateTimeZone value with one of the available PHP timezones.

提交回复
热议问题