Differences between the date and gmdate functions in php _ PHP Tutorial

Source: Internet
Author: User
Tags http cookie
The difference between the date function and gmdate function in php. PHP has two time formatting functions: date () and gmdate (). the description in the official document is date format a local time/date gmdate format a GMTUTC date/time, two time formatting functions are returned for PHP: date () and gmdate (), the description in the official document is 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

The code is as follows:

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.


PHP Date/Time constant
PHP: indicates the earliest PHP version that supports this constant.

Constant description PHP

DATE_ATOM atomic clock format (for example, 2005-08-15T16: 13: 03 + 0000)
DATE_COOKIE HTTP cookie format (for example, Sun, 14 Aug 2005 16:13:03 UTC)
DATE_ISO8601 ISO-8601 (e.g.: 2005-08-14T16: 13: 03 + 0000)
DATE_RFC822 RFC 822 (for example, Sun, 14 Aug 2005 16:13:03 UTC)
DATE_RFC850 RFC 850 (for example, Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1036 RFC 1036 (for example, Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1123 RFC 1123 (for example, Sun, 14 Aug 2005 16:13:03 UTC)
DATE_RFC2822 RFC 2822 (e.g., Sun, 14 Aug 2005 16:13:03 + 0000)
DATE_RSS (for example, Sun, 14 Aug 2005 16:13:03 UTC)
DATE_W3C World Wide Web Consortium (for example, 2005-08-14T16: 13: 03 + 0000)


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 );

Gmdate (): returns the "custom format" time of the current GMT standard time, regardless of the time zone set by the php system.

Example 1

When the following program is run in China (GMT + 0800), the first line shows "Jan 01 2000 00:00:00", and the second line shows "Dec 31 1999 16:00:00 ".

The code is as follows:

Echo date ("M d y h: I: s", mktime ));
Echo gmdate ("M d y h: I: s", mktime ));
?>

Output:

Jan 01 00:00:00 2000
Dec 31 1999 16:00:00

Example 2

The code is as follows:

Echo ("Result with date ():
");
Echo (date ("l ")."
");
Echo (date ("l dS of f y h: I: s ")."
");
Echo ("Oct 3,1975 was on a". date ("l", mktime ))."
");
Echo (date (DATE_RFC822 )."
");
Echo (date (DATE_ATOM, mktime ))."

");

Echo ("Result with gmdate ():
");
Echo (gmdate ("l ")."
");
Echo (gmdate ("l dS of f y h: I: s ")."
");
Echo ("Oct 3,1975 was on a". gmdate ("l", mktime ))."
");
Echo (gmdate (DATE_RFC822 )."
");
Echo (gmdate (DATE_ATOM, mktime ))."
");
?>

Output:

Result with date ():
Tuesday
Tuesday 24th of January 2006 02:41:22
Oct 3,1975 was on a Friday
Tue, 24 Jan 2006 14:41:22 CET
1975-10-03T00: 00: 00 + 0100

Result with gmdate ():
Tuesday
Tuesday 24th of January 2006 01:41:22
Oct 3,1975 was on a Thursday
Tue, 24 Jan 2006 13:41:22 GMT
1975-10-02T23: 00: 00 + 0000


Values () and gmdate () are described in the official document as date. Format a local time/date gmdate to format a GMT/UTC date/time, and return...

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.