Date-format a local time/date
The date/time function allows you to extract and format the date and time on the server.
Note: these functions depend on the local settings of the server.
Install
The date/time function is a core component of PHP. These functions can be used without installation.
Runtime configuration
The behavior of the date/time function is affected by the settings in php. ini.
Date/Time configuration options:
The default name description can be changed.
Date. default_latitude "31.7667" specifies the default latitude (available starting from PHP 5 ). Date_sunrise () and date_sunset () use this option. PHP_INI_ALL
Date. Default_longpolling "35.2333" specifies the default longitude (available from PHP 5 ). Date_sunrise () and date_sunset () use this option. PHP_INI_ALL
Date. sunrise_zenith "90.83" specifies that Sunday is available from PHP 5 ). Date_sunrise () and date_sunset () use this option. PHP_INI_ALL
Date. sunset_zenith "90.83" specifies the sunset ceiling (available starting from PHP 5 ). Date_sunrise () and date_sunset () use this option. PHP_INI_ALL
Date. timezone "" specifies the default time zone (available from PHP 5.1 ). PHP_INI_ALL
The gmdate () function format the GMT/UTC date/time.
Similar to the date () function, the difference is that the return time is Greenwich Mean Time (GMT ).
Syntax
Gmdate (format, timestamp)
Parameter description
Format is optional. Specifies how results are returned.
Optional.
Tips and comments
Note: A negative timestamp (date before January 1, 1970) cannot work in some systems (such as Windows) before PHP 5.1.0.
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
The code is as follows: |
Copy code |
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:
The code is as follows: |
Copy code |
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.