How can I get the local server's time zone?

前端 未结 6 1528
庸人自扰
庸人自扰 2021-01-02 01:53

How can I get the local server\'s timezone without relying on php.ini or other configuration files?

I am looking for an output similar to the output of

6条回答
  •  轮回少年
    2021-01-02 02:17

    You don't actually need to know what "timezone" MySQL is running on - you merely need to know the difference to UTC.

    To get that you can simply ask MySQL:

    SELECT TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP, NOW())
    

    Since the value of NOW() is based on the timezone MySQL is running on, that will read the difference between NOW() and the UTC time, in hours. You can then use that value to create a full timestamp like 2016-04-15T15:52:01+01:00 which can be used in DateTime::__construct().

    You can then let PHP worry about the difference the timezones between the application and database servers by comparing DateTime objects... and that should work on all systems.

提交回复
热议问题