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 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"
0 comments:
Post a Comment