What's the best way to manage dates across PHP, MySQL, etc?

前端 未结 4 637
渐次进展
渐次进展 2021-01-03 03:52

My server is in Dallas. I\'m in New York City.. and both PHP and MySQL have configuration variables for setting the timezone.

How do I get them all to work together

4条回答
  •  迷失自我
    2021-01-03 04:38

    The mysql-server stores dates in a timezone independent format (UTC).
    But before it stores the date it will be converted using its timezone.

    U can change the mysql timezone per connection *1:

    mysql_query('SET time_zone = "'.$timezone.'"');
    

    You can also change the timezone per script.

    date_default_timezone_set($timezone);
    

    If you set them to the same timezone "2009-01-10 13:30:00" will mean the same thing to both mysql and php.

    But keep in mind that the 2 servers have different internal clock values, so if you want to generate timestamps based on current time. Do that in mysql OR php.

    *1) MySQL timezone support may require additional configuration. check the manual

提交回复
热议问题