Home  >  Article  >  Backend Development  >  Solution to date time difference of 8 hours in php

Solution to date time difference of 8 hours in php

PHP中文网
PHP中文网Original
2016-07-25 08:46:413076browse

Solution to the 8-hour difference in date time in php

On Windows, under the default PHP configuration, the time value returned by the date function is always 8 hours different from the local time, that is The time shown is Greenwich Mean Time. The solution is to set PHP's timezone environment correctly.

PHP’s time zone environment is specified through a specific set of strings. See the list of time zones supported by PHP.

The easiest way is to find the date.timezone line in the php.ini file. By default, this line is commented out. Delete the preceding semicolon to enable it. For mainland China, set it to Etc/GMT-8 (or Asia/Sanghai, Asia/Chongqing, Asia/Urumqi, PRC).

If you cannot modify php.ini for some reason, you can use date_default_timezone_set() to set it in the PHP program. For example, you can execute

date_default_timezone_set('Etc/GMT-8');

before using the date function. Personally, I think it is better to use the date_default_timezone_set function. First, it is highly versatile, and secondly, if your website is for global users, this method allows each user to set his or her time zone.

The last thing that needs to be explained is the writing method of Etc/GMT. Mainland China uses the time in the East 8 Zone, which is usually written as +8, but there is a big change in PHP, which needs to be written as Etc/GMT-8; on the contrary, if your time zone is the West 2 Zone , it should be written as Etc/GMT+2 in PHP. This is different from common sense, so we should pay special attention to it.


The above is the solution to the 8-hour difference in date time in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!






Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn