Home >Backend Development >PHP Tutorial >Solution to the problem that the date() function in PHP5.2 displays a time difference of 8 hours from Beijing time_PHP Tutorial
Question:
PHP function date("Y-n-d H-i-s"); The output time is 8 hours different from the local time.
Solution ideas
Beijing time is in the East 8th District, which may be a time zone issue. But it was no problem before. After searching through Google, I found that starting from php5.1.0, the date.timezone option was added to php.ini, which is turned off by default. , that is, the time displayed (no matter what php command is used) is Greenwich Mean Time, which is exactly 8 hours different from our time (Beijing time).
Set the time zone before calling the date() function.
Method 1:
date_default_timezone_set('PRC');
No return value. It’s OK to use the date function directly below
$time = date(Y."Year".m."Month".d."Day".G."Hour".i."Min");
Method 2:
Modify;date.timezone = in php.ini to date.timezone = "Asia/Shanghai" (or change to date.timezone = PRC)and remove the preceding semicolon, restart Apache. Can.