PHP Date - Reference
Important Full Date and Time:
* r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")
Time:
* a: am or pm depending on the time
* A: AM or PM depending on the time
* g: Hour without leading zeroes. Values are 1 through 12.
* G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
* h: Hour with leading zeroes. Values 01 through 12.
* H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
* i: Minute with leading zeroes. Values 00 through 59.
* s: Seconds with leading zeroes. Values 00 through 59.
Day:
* d: Day of the month with leading zeroes. Values are 01 through 31.
* j: Day of the month without leading zeroes. Values 1 through 31
* D: Day of the week abbreviations. Sun through Sat
* l: Day of the week. Values Sunday through Saturday
* w: Day of the week without leading zeroes. Values 0 through 6.
* z: Day of the year without leading zeroes. Values 0 through 365.
Month:
* m: Month number with leading zeroes. Values 01 through 12
* n: Month number without leading zeroes. Values 1 through 12
* M: Abbreviation for the month. Values Jan through Dec
* F: Normal month representation. Values January through December.
* t: The number of days in the month. Values 28 through 31.
Year:
* L: 1 if it`s a leap year and 0 if it isn`t.
* Y: A four digit year format
* y: A two digit year format. Values 00 through 99.
Other Formatting:
* U: The number of seconds since the Unix Epoch (January 1, 1970)
* O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
Common formats
MySQL date format:
------------------
date(`Y-m-d H:i:s`)
- or, for date only: -
date(`Y-m-d`)
This is the format to use in MySQL SQL statements to express dates in string form.
The documentation does not specify a way to indicate a time zone; the date should be in the time zone that the MySQL server is set to use in order to be correct. If PHP and MySQL are on the same server then date() should normally already express dates using the same time zone as MySQL.
HTTP header date format:
------------------------
gmdate(`D, d M Y H:i:s GMT`)
This is the format to use for HTTP headers such as Date:, Expires: and Last-Modified:, but not the Set-Cookie:, header which uses a different format, shown in the next section.
Note that this uses the gmdate() function, which is identical to date() except it converts the date to the GMT time zone; also, PHP may report GMT as `UTC`, however the standard requires that UTC always be represented as `GMT`.
Context example:
HTTP/1.1 200 OK
Date: Mon, 19 Nov 2007 23:47:33 GMT
Server: Apache/1.3.33 (Darwin) PHP/4.4.7
Last-Modified: Mon, 19 Nov 2007 23:40:02 GMT
Accept-Ranges: bytes
HTTP cookie date format:
------------------------
gmdate(`D, d-M-Y H:i:s GMT`)
This format is for the expiration date of HTTP cookies, and is equivalent to the toGMTString() method of date objects in javascript. Note that this also uses gmdate() and forces `GMT` instead of `UTC` as the reported time zone, as required by the specification.
Context example: Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net
RSS date format:
----------------
date(`D, d M Y H:i:s T`)
- or -
date(`D, d M Y H:i:s O`)
This date format is used in RSS feeds, in XML elements such as pubDate or lastBuildDate. The date format must be RFC 822 compliant but a 4 digit year is preferred. Unlike the format used by HTTP headers, GMT is not required, and the time zone may be expressed using the either time zone abbreviation (`T`) or the offset from GMT (`O`).
Context example:
<rss version="2.0">
<channel>
<title>Apple Hot News</title>
<link>http://www.apple.com/hotnews/</link>
<description>Hot News provided by Apple.</description>
<language>en-us</language>
<copyright>Copyright 2007, Apple Inc.</copyright>
<pubDate>Fri, 16 Nov 2007 14:47:14 PST</pubDate>
<lastBuildDate>Fri, 16 Nov 2007 14:47:14 PST</lastBuildDate>
Atom date format:
-----------------
date(`c`) (PHP 5)
- or -
gmdate(`Y-m-dTH:i:s`)
Dates in atom files must be compliant with RFC 3339. GMT/UTC is not required.
For versions of PHP earlier than 5.0 and therefore lacking the handy `c` specifier for ISO 8601 dates, of which RFC 3339 is a subset, getting the time zone specified correctly was tricky for non-GMT dates. Forcing GMT and denoting it as `Z` will work in all PHP versions.
Context example:
<feed xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-08">
<title>Example Feed</title>
<link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
<author> <name>John Doe</name> </author>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
Unix/C date format:
-------------------
date(`D M j G:i:s T Y`)
This is the date format as reported by unix utilities such as date and not coincidentally the format used by ANSI C`s asctime() function. The equivalent in the format used by the date utility, as well as the C and alias PHP function strftime(): `%a %b %e %H:%M:%S %Z %Y`.
Example: Mon Nov 19 19:03:52 EST 2007
mktime()
mktime()
mktime ( [hour [, minute [, second [, month [, day [, year [, daylight savings: 0 for no dst; 1 for dst; -1 dst unknown]]]]]]] )
Examples:
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
Note that these examples will handle daylight savings appropriately as well as overflows, e.g. November 32 will be interpreted as December 1.