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 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: |
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.
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: |
Copy code |
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: |
Copy code |
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 |