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

前端 未结 4 674
渐次进展
渐次进展 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:27

    Use Unix Time everywhere. It's using UTC so it's the same for every timezone. Methods for dates usually convert to it and back from it using timezone information they have, so you would have yourself a correct time.

    Alternatively you could use Unix Time only to transfer time from one computer to another (like from DB to your server running PHP, or to JavaScript client). There's functions to convert to it and from it in every language. For MySQL it is:

    UNIX_TIMESTAMP(date)
    FROM_UNIXTIME(unix_timestamp)
    

    That way you could have your time properly formatted on the DB and in logs but still have correct local time everywhere.

提交回复
热议问题