The time in PHP has two formatting functions: Date () and gmdate (), which are described in the official document as follows:
Date-format a local time/date
Gmdate-format a gmt/UTC date/time and return the Greenwich Mean Time (GMT ).
For example, if the current time zone is + 8, the time returned by the server running the following script should be as follows:
The current time is assumed to be 12:15:27
Echo date ('Y-m-d h: I: s', time (); output: 12:15:27
Echo gmdate ('Y-m-d h: I: s', time (); output: 04:15:27
However, this is only the result obtained by running PHP in Linux + Apache. If it is run in windows, the two functions will return the following results: 04:15:27.
Therefore, we should give a compatibility method, use gmdate in a unified manner, and manually set the current time zone. The improvement is as follows:
Echo gmdate ('Y-m-d h: I: s', time () + 3600*8 );
In this way, both Linux + Apache and windows get the correct results. Of course, this write method also has the advantage that when the website is for the whole world, you only need to set the time zone where the Website user is located, the program automatically calculates the time based on the time zone set by the user. The database only saves the time generated by the current time (). The release time displayed in the China + 8 time zone is: 12:15:27, the release time of this information displayed by users in the European + 2 time zone is: 06:15:27, so that all the information time is correct.
========================================================== ================
Modify the default time zone of PHP
Each region has its own local time. In online and radio communication, the time conversion problem is particularly prominent. The whole earth is divided into twenty-four time zones, each of which has its own local time. For international radio or network communications, a unified Time (UTC, Universal Time Coordinated) is used to ensure uniformity. It is a global standard time set by the World Time Standard. UTC, also known as Greenwich Mean Time (GMT, Greenwich Mean Time), is the same as local time in London, England.
The Default Time Zone of PHP is UTC time, while Beijing is located in the GMT + 8, eight hours ahead of UTC. So when using a function like time () in PHP to obtain the current time, the time is always incorrect, Which is eight hours different from Beijing time. If you want to correctly display the Beijing time, you need to modify the default time zone settings. You can use either of the following methods.
If you are using an independent server and have the permission to modify the configuration file, you can set the time zone by modifying the date. timezone attribute in PHP. ini. We can set the value of this property to one of "Asia/Shang", "Asia/Chongqing", "ETC/GMT-8", or PRC, the current time obtained in the PHP script is Beijing time. Modify the PHP configuration file as follows:
Date. timezone = ETC/GMT-8
// In the configuration file, set the default time zone to Zone 8 (Beijing time)
If you are using a shared server, you are not authorized to modify the configuration file PHP. ini, And the PHP version is later than 5.1.0, you can also call the date_default_timezone_set () function to set the time zone before the output time. This function requires a Time Zone identifier as a parameter, which is the same as the value of the date. timezone attribute in the configuration file. The function is used as follows:
Date_default_timezone_set ('prc ');
// Set the time zone before the output time. PRC is the echo date ('Y-m-d h: I: s', time () of the People's Republic of China ());
// The current output time is Beijing time.
Test:
Code
<? PHP
Date_default_timezone_set ('etc/gmt ');
// Set the time zone before the output time. ETC/GMT is the standard time.
Echo (Time ());
Echo ('<br/> ');
Echo date ('Y-m-d h: I: s', time ());
Echo ('<br/> ');
Echo gmdate ("Y-m-d h: I: S", time ());
Echo ('<br/> ');
Date_default_timezone_set ('prc ');
// Set the time zone before the output time. The PRC is the People's Republic of China.
Echo (Time ());
Echo ('<br/> ');
Echo date ('Y-m-d h: I: s', time ());
Echo ('<br/> ');
Echo gmdate ("Y-m-d h: I: S", time ());
?>
Result:
1276257131
2010-06-11 11:52:11
2010-06-11 11:52:11
1276257131
2010-06-11 19:52:11
2010-06-11 11:52:11
Conclusion:
Time (): Standard Timestamp
Returns the time from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970)Current time (the current GMT Standard Time)In seconds,The value is irrelevant to the time zone set by the PHP system..
Date ():The currentGMT Standard TimeThe "custom format" time of the localization timePHP SystemThe time zone.
Gmdate ():The currentGMT Standard TimeThe "custom format" timePHP SystemThe specified time zone is irrelevant.
========================================================== ==================
UNIX timestamp:Refers to the number of seconds between the current (GMT standard) time and the Unix epoch time (00:00:00;
Code
<? PHP
Date_default_timezone_set ('etc/gmt ');
// Set the time zone before the output time. ETC/GMT is the standard time.
Echo (strtotime ('2017-01-01 08:00:00 ′));
Echo ('<br/> ');
Date_default_timezone_set ('prc ');
// Set the time zone before the output time. The PRC is the People's Republic of China.
Echo (strtotime ('2017-01-01 08:00:00 ′));
?>
Test results:
28800
0
Strtotime ():The UNIX timestamp of The GMT Standard Time.PHP SystemIs not the GMT Standard Time ZoneStrtotime ()The system automatically converts time A to the corresponding GMT Standard Time, And then calculates the Unix timestamp of this time. AndPHP SystemTime Zone