Php: An error occurred while retrieving the current time using the date function. PHP is a beginner who wants to obtain the current time on the page. anyone who has learned programming knows to use the time function date () to format a local time/date with this function, first, write a test code for the beginner PHP to build a website. if you want to obtain the current time on the page, anyone who has learned programming knows to use the time function date (). First, use this function to format a local time/date, write a test code first. The result output time is eight hours earlier than the actual time. why:
[Php]
Echo date ('Y-m-d H: I: s ');
? > 〉
Echo date ('Y-m-d H: I: s ');
? > 〉
Output Current time: 02:32:17
The actual time is 10:32:17.
Is it because the date () time in PHP is incorrect and 8 hours less?
Let's take a look at the "Example 1. date () example" in the PHP Manual. The first line has a time zone setting.
// Set the default time zone to use. Available in PHP 5.1
Date_default_timezone_set ('utc ');
Php. date is added to ini. the timezone option is disabled by default, that is, the displayed time (no matter what php command is used) is Greenwich Mean Time, which is exactly eight hours behind Beijing time.
How to set the correct PHP time.
1. modify php. ini. Open php. ini to search for date. timezone and remove the semicolon = followed by Asia/Shanghai. restart the apache server. The disadvantage is that if the program
Put it on someone else's server and cannot modify php. ini.
2. add the initialization statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will. I recommend this statement.
Time zone identifier, available value in mainland China: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, asia/Harbin
Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei) and Singapore: Asia/Singapore
In this way, the output is Beijing time.
For more details about the time zone code, the time zone code, the time zone index code timezone_identifier, which can be found on the official website.
Begin (), first use this function to format a local time/date, first write a test generation...