Difference between php date () and gmdate ()

Source: Internet
Author: User

Here we will introduce the differences between the date functions in php. Here we mainly talk about the differences in the usage of date and gmdate. If you need them, please refer to them.

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.