Home >Backend Development >PHP Tutorial >Solution to why the time obtained by date() in php5 is not the current time
Starting from php5.10, time zone settings have been added to php. The time displayed in php is Greenwich Mean Time. This has caused a problem of eight hours difference for our Chinese users! [Recommended tutorial: php introductory tutorial]
The relevant setting is to modify the date.timezone parameter in php.ini:
[Date] ; Defines the default timezone used by the date functions ;date.timezone =
The default is closed, just remove the comment and change it to
[Date] ; Defines the default timezone used by the date functions date.timezone = PRC
where PRC is "The People's Republic of China"!
For other options, please refer to the php manual.
But the Asian region above misses our capital Beijing. I don’t know if the foreigner did it on purpose!
If you do not have permission to modify php.ini, you only need to call date_default_timezone_set ('
PRC') when calling the time and date function!
You can also call date_default_timezone_get() to view the current time zone setting!
About XXX, the available values in mainland China are:
Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order Chongqing, Shanghai, Urumqi)
Available values in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia /Taipei (Macau, Hong Kong, Taipei in order)
Taiwan area can be set to: date.timezone = "Asia//Taipei"
And Singapore: Asia/Singapore
Solution to the eight-hour time difference in PHP5
After installing php5, I accidentally saw someone on the forum saying that the time display of php5.1.2 was 8 hours shorter.
<?php echo date("Y-m-d H:i:s"); ?>
The result was that the difference was 8 hours after my own test.
Later, after looking for information on the forum, the solution was finally solved. In php5 and above versions, to output the local time (only in China)
, you can write the code like this:
<?php date_default_timezone_set('Asia/Shanghai'); echo date('Y-m-d H:i:s'); ?>
You can also write the code like this:
<?php date_default_timezone_set('Asia/Chongqing'); echo date('Y-m-d H:i:s'); ?>
This solves the problem of eight hours difference in time! ! ~~~