Home >Backend Development >PHP Tutorial >Description of the difference between php date() and gmdate()_PHP tutorial
Here we will introduce the differences between PHP date functions. Here we mainly talk about the usage differences between date and gmdate. Friends in need can refer to it.
date — Format a local time/date
The date/time functions allow you to extract and format the date and time on the server.
Note: These functions rely on the local settings of the server.
Install
Date/time functions are an integral part of PHP core. No installation is required to use these functions.
Runtime configuration
The behavior of date/time functions is affected by settings in php.ini.
Date/Time configuration options:
Name Default Description Can be changed
date.default_latitude "31.7667" specifies the default latitude (available since PHP 5). date_sunrise() and date_sunset() use this option. PHP_INI_ALL
date.default_longitude "35.2333" specifies the default longitude (available since PHP 5). date_sunrise() and date_sunset() use this option. PHP_INI_ALL
date.sunrise_zenith "90.83" specifies sunrise zenith (available since PHP 5). date_sunrise() and date_sunset() use this option. PHP_INI_ALL
date.sunset_zenith "90.83" specifies the sunset zenith (available since PHP 5). date_sunrise() and date_sunset() use this option. PHP_INI_ALL
date.timezone "" specifies the default time zone (available since PHP 5.1). PHP_INI_ALL
gmdate() function formats GMT/UTC date/time.
Similar to the date() function, except that the time returned is Greenwich Mean Time (GMT).
Grammar
gmdate(format,timestamp)
Parameter Description
format is optional. Specifies how results are returned.
timestamp is optional.
Tips and Notes
Note: Prior to PHP 5.1.0, negative timestamps (dates before 1970) did not work on some systems (e.g. Windows).
For example, our current time zone is +8, then the time returned by the server running the following script should be like this:
The current time is assumed to be 2007-03-14 12:15:27
The code is as follows | Copy code | ||||
echo gmdate(‘Y-m-d H:i:s’, time()); The output is: 2007-03-14 04:15:27
|
代码如下 | 复制代码 |
echo gmdate(‘Y-m-d H:i:s’, time() + 3600 * 8); |
Therefore, we should give a compatible writing method, use gmdate uniformly, and set the current time zone manually. The writing method is improved as follows:
The code is as follows | Copy code |
echo gmdate(‘Y-m-d H:i:s’, time() + 3600 * 8); |
In this way, the correct results are obtained regardless of whether it is under Linux+Apache or Windows. Of course, there is another advantage of writing this way. When the website is for the whole world, the website user only needs to set the time zone, and the program will automatically adjust the time zone according to the user's location. The set time zone is used for time calculation. The information release time in the database only stores the time generated by the current time(). Then the release time seen in China +8 time zone is: 2007-03-14 12:15:27, then in Users in the European +2 time zone will see that the release time of this information is: 2007-03-14 06:15:27, so that all the times of the information will be correct.
TechArticleHere we will introduce the difference between php date functions. Here we mainly talk about the usage difference between date and gmdate. If necessary Friends can refer to it. date formats a local time/date...