Home  >  Article  >  Backend Development  >  The difference between php function date and gmdate_PHP tutorial

The difference between php function date and gmdate_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:231081browse

PHP has two time formatting functions: date() and gmdate(). The description in the official documentation is date - formats a local time/date gmdate - formats a GMT/UTC date/time, and returns Green U.K. Standard Time (GMT). ​

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 date(‘Y-m-d H:i:s’, time()); 输出为:2007-03-14 12:15:27

echo gmdate(‘Y-m-d H:i:s’, time()); 输出为:2007-03-14 04:15:27

echo date(‘Y-m-d H:i:s’, time()); The output is: 2007-03-14 12:15:27

echo gmdate(‘Y-m-d H:i:s’, time()); The output is: 2007-03-14 04:15:27


But this is only the result of running PHP under Linux+Apache. If it is run under Windows, both functions return: 2007-03-14 04:15:27.

PHP Date/Time constants

PHP: Indicates the earliest PHP version that supports this constant.


Constant Description PHP

DATE_ATOM atomic clock format (eg: 2005-08-15T16:13:03+0000)
DATE_COOKIE HTTP Cookies format (eg: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_ISO8601 ISO-8601 (eg: 2005-08-14T16:13:03+0000)
DATE_RFC822 RFC 822 (eg: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_RFC850 RFC 850 (eg: Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1036 RFC 1036 (eg: Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1123 RFC 1123 (eg: Sun, 14 Aug 2005 16:13:03 UTC)

DATE_RFC2822 RFC 2822 (eg: Sun, 14 Aug 2005 16:13:03 +0000)

DATE_RSS RSS (eg: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_W3C World Wide Web Consortium (eg: 2005-08-14T16:13:03+0000)

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:

echo gmdate(‘Y-m-d H:i:s’, time() + 3600 * 8);

gmdate(): Returns the "custom format" time of the current GMT standard time, which has nothing to do with the time zone set by the PHP system.

Example 1
 代码如下 复制代码

echo date("M d Y H:i:s", mktime (0,0,0,1,1,2000));
echo gmdate("M d Y H:i:s", mktime (0,0,0,1,1,2000));
?>

输出:

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

When running the following program in China (GMT +0800), the first line displays "Jan 01 2000 00:00:00" while the second line displays "Dec 31 1999 16:00:00".
The code is as follows Copy code
echo date("M d Y H:i:s", mktime (0,0,0,1,1,2000)); echo gmdate("M d Y H:i:s", mktime (0,0,0,1,1,2000)); ?> Output: Jan 01 2000 00:00:00 Dec 31 1999 16:00:00

例子 2

 代码如下 复制代码
 代码如下 复制代码

echo("Result with date():
");
echo(date("l") . "
");
echo(date("l dS of F Y h:i:s A") . "
");
echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."
");
echo(date(DATE_RFC822) . "
");
echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "

");

echo("Result with gmdate():
");
echo(gmdate("l") . "
");
echo(gmdate("l dS of F Y h:i:s A") . "
");
echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."
");
echo(gmdate(DATE_RFC822) . "
");
echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "
");
?>

输出:

Result with date():
Tuesday
Tuesday 24th of January 2006 02:41:22 PM
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 PM
Oct 3,1975 was on a Thursday
Tue, 24 Jan 2006 13:41:22 GMT
1975-10-02T23:00:00+0000

echo("Result with date():
");
echo(date("l") . "
");
echo(date("l dS of F Y h:i:s A") . "
");
echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."
");
echo(date(DATE_RFC822) . "
");
echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "

");
echo("Result with gmdate():
");
echo(gmdate("l") . "
");
echo(gmdate("l dS of F Y h:i:s A") . "
");
echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."
");
echo(gmdate(DATE_RFC822) . "
");
echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "
");
?>

输出:Result with date():
Tuesday
Tuesday 24th of January 2006 02:41:22 PM
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 PM
Oct 3,1975 was on a Thursday
Tue, 24 Jan 2006 13:41:22 GMT
1975-10-02T23:00:00+0000
http://www.bkjia.com/PHPjc/445298.htmlwww.bkjia.comtrue
http://www.bkjia.com/PHPjc/445298.html
TechArticlePHP有2个时间格式化函数:date()和gmdate(),在官方的文档中的描述为date 格式化一个本地时间/日期gmdate 格式化一个 GMT/UTC 日期/时间,返回...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn