PHP Date Constants
Posted August 16th, 2008 in PHP
When writing my "Get a list of all available constants with PHP" post I discovered there are a number of usful date format constants in PHP which can be used with the date() function. (Refer to my "Formatting Dates with PHP" post for more details about the PHP date function). This post looks at these constants and how to use them with the date function. Please note that these constants have only been present in PHP since 5.1.1.
Example usage
The following example will get the current date and time in the valid format for an RSS feed and assign it to the $date variable:
$date = date(DATE_RSS);
Date constants
The available date constants are listed below with offsite links to the appropriate RFC or ISO standard which define it, where applicable. Please note that these constants are available only from PHP version 5.1.1. The datetimes in the examples below are from the original post date of this article in New Zealand standard time.
DATE_ATOM
This is the format for Atom feeds. The PHP format is "Y-m-d\TH:i:sP" and example output from date(DATE_ATOM) is "2008-08-16T12:00:00+12:00"
DATE_COOKIE
This is the format for cookies set from a web server or Javascript. The PHP format is "l, d-M-y H:i:s T" and example output from date(DATE_COOKIE) is "Sat, 16 Aug 2008 12:00:00 NZST"
DATE_ISO8601
This is the format for ISO8601, an international date and time format standard. The PHP format is "Y-m-d\TH:i:sO" and example output from date(DATE_ISO8601) is "2008-08-16T12:00:00+1200". Read about ISO8601 at Wikipedia.
DATE_RFC822
This is the format for RFC822 which defines the standards for email messages. The PHP format is "D, d M y H:i:s O" and example output from date(DATE_RFC822) is "Sat, 16 Aug 2008 12:00:00 NZST". Read about RFC822 at faqs.org
DATE_RFC850
This is the format for RFC850 which defines the standards for USENET messages. The PHP format is "l, d-M-y H:i:s T" and example output from date(DATE_RFC850) is "Saturday, 16-Aug-08 12:00:00 NZST". Read about RFC850 at faqs.org
DATE_RFC1036
This is the format for RFC1036, a later definition for USENET. The PHP format is "l, d-M-y H:i:s T" (the same as for DATE_RFC850) and example output from date(DATE_RFC1036) is "Saturday, 16-Aug-08 12:00:00 NZST". Read about RFC1036 at faqs.org
DATE_RFC1123
This is the format for RFC1123 and covers requirements for Internet hosts. The PHP format is "D, d M Y H:i:s T" and example output from date(DATE_RFC1123) is "Sat, 16 Aug 2008 12:00:00 NZST" Read about RFC1123 at faqs.org
DATE_RFC2822
This is the format for RFC2822 which updates RFC822 for email messages. The PHP format is "D, d M Y H:i:s O" and example output from date(DATE_RFC2822) is "Sat, 16 Aug 2008 12:00:00 +1200" Read about RFC2822 at faqs.org
DATE_RFC3339
This is the format for RFC3339 and defines "date and time on the Internet". The PHP format is "Y-m-d\TH:i:sP" and example output from date(DATE_RFC3339) is "2008-08-16T12:00:00+12:00" Read about RFC3339 at faqs.org
DATE_RSS
This is the format for RSS feeds. The PHP format is "D, d M Y H:i:s T" and example output from date(DATE_RSS) is "Sat, 16 Aug 2008 12:00:00 NZST"
DATE_W3C
This is the format for "World Wide Web Consortium" according to the PHP documentation, although I'm not sure what this actually means. The PHP format is "Y-m-d\TH:i:sP" and example output from date(DATE_W3C) is "2008-08-16T12:00:00+12:00"
Related posts:
- Get a list of all available constants with PHP (Thursday, July 17th 2008)
- Using strtotime with PHP (Friday, December 21st 2007)
- Formatting Dates with PHP (Thursday, December 20th 2007)
Recent posts:
- MySQL queries for article summaries part 2 of 2 (Tuesday, January 6th 2009)
- Aims for 2009 (Monday, January 5th 2009)
- Weekly Roundup - January 5th 2008 (Monday, January 5th 2009)
- MySQL queries for article summaries part 1 of 2 (Sunday, January 4th 2009)
- 2008 Summary of Posts (Saturday, January 3rd 2009)
- 2008 / 2009 overview (Friday, January 2nd 2009)
Subscribe to RSS Feed / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email and have my posts sent in a daily email.
Posts are made using the following schedule (although it may vary some weeks): Mondays & Fridays = PHP; Tuesdays & Saturdays = MySQL; Wednesdays & Sundays = Javascript/jQuery; Thursdays = HTML/CSS.
