Home >Backend Development >PHP Tutorial >Solution to why the time obtained by date() in php5 is not the current time_PHP 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 off Yes, just remove the comment and change it to
[Date]
; Defines the default timezone used by the date functions
date.timezone = PRC
where PRC is "中华People's Republic"!
For other options, please refer to the php manual.
However, our capital Beijing is omitted from the Asian region above. 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!
Regarding XXX, the available values in mainland China are:
Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order, Chongqing, Shanghai, Urumqi)
Hong Kong and Taiwan Available regions: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in order)
Taiwan region can be set to: date.timezone = "Asia//Taipei"
Also Singapore: Asia /Singapore
Solution to the eight-hour time difference in PHP5
After installing php5, I accidentally saw someone on the forum saying php5.1.2 The time display is 8 hours shorter,
echo date("Y-m-d H:i:s");
?>
The result of my own test is that the difference is indeed 8 hours.
Later, after searching for information on the forum, the result 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 code like this:
date_default_timezone_set('Asia/Chongqing');
echo date('Y-m-d H:i:s');
?>
This way the time is eight hours apart The problem is solved!!~~~