RSS / ATOM 的格式要产生都很简单, 麻烦的是日期格式的产生(RSS => pubDate, ATOM => created).
发现 PHP Date 有提供这些需要的相关全域变量可以使用:(PHP 5.1.1 以后开始支援这些全域变量)
- echo DATE_RSS; // D, d M Y H:i:s O
- echo DATE_ATOM; // Y-m-d\TH:i:sP
- echo date(DATE_RSS); // Wed, 23 Apr 2008 19:27:19 +0800
- echo date(DATE_ATOM); // 2008-04-23T19:27:38+08:00
如果是 MySQL timestamp/datetime format 直接拉出来的资料, 就可以直接使用:
- date(DATE_RSS, strtotime($MYSQL_DATETIME_DATA));
目前支持的日期全域变量有以下:
- DATE_ATOM: Atom Atom (example: 2005-08-15T15:52:01+00:00)
- DATE_COOKIE: HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC)
- DATE_ISO8601: ISO-8601 (example: 2005-08-15T15:52:01+0000)
- DATE_RFC822: RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000)
- DATE_RFC850: RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC)
- DATE_RFC1036: RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000)
- DATE_RFC1123: RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_RFC2822: RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_RFC3339: Same as DATE_ATOM
- DATE_RSS: RSS (Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_W3C: World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00)
PHP 预定义的日期格式
自 PHP 5.1.1 起定义有以下常量来提供标准日期表达方法,可以用于日期格式函数
- echo DATE_RSS; // D, d M Y H:i:s O
- echo DATE_ATOM; // Y-m-d\TH:i:sP
- echo date(DATE_RSS); // Wed, 23 Apr 2008 19:27:19 +0800
- echo date(DATE_ATOM); // 2008-04-23T19:27:38+08:00
虽然格式简单, 但用PHP内的预定义格式岂不更加方便.
目前PHP所有支持的日期格式:
- DATE_ATOM: Atom Atom (example: 2005-08-15T15:52:01+00:00)
- DATE_COOKIE: HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC)
- DATE_ISO8601: ISO-8601 (example: 2005-08-15T15:52:01+0000)
- DATE_RFC822: RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000)
- DATE_RFC850: RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC)
- DATE_RFC1036: RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000)
- DATE_RFC1123: RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_RFC2822: RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_RFC3339: Same as DATE_ATOM
- DATE_RSS: RSS (Mon, 15 Aug 2005 15:52:01 +0000)
- DATE_W3C: World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00)
PHP Date / Time Functions
PHP: indicates the earliest version of PHP that supports the function.
Function | Description | PHP |
---|---|---|
checkdate() | Validates a Gregorian date | 3 |
date_default_timezone_get() | Returns the default time zone | 5 |
date_default_timezone_set() | Sets the default time zone | 5 |
date_sunrise() | Returns the time of sunrise for a given day / location | 5 |
date_sunset() | Returns the time of sunset for a given day / location | 5 |
date() | Formats a local time/date | 3 |
getdate() | Returns an array that contains date and time information for a Unix timestamp | 3 |
gettimeofday() | Returns an array that contains current time information | 3 |
gmdate() | Formats a GMT/UTC date/time | 3 |
gmmktime() | Returns the Unix timestamp for a GMT date | 3 |
gmstrftime() | Formats a GMT/UTC time/date according to locale settings | 3 |
idate() | Formats a local time/date as integer | 5 |
localtime() | Returns an array that contains the time components of a Unix timestamp | 4 |
microtime() | Returns the microseconds for the current time | 3 |
mktime() | Returns the Unix timestamp for a date | 3 |
strftime() | Formats a local time/date according to locale settings | 3 |
strptime() | Parses a time/date generated with strftime() | 5 |
strtotime() | Parses an English textual date or time into a Unix timestamp | 3 |
time() | Returns the current time as a Unix timestamp | 3 |
PHP Date / Time Constants
PHP: indicates the earliest version of PHP that supports the constant.
Constant | Description | PHP |
---|---|---|
DATE_ATOM | Atom (example: 2005-08-15T16:13:03+0000) | |
DATE_COOKIE | HTTP Cookies (example: Sun, 14 Aug 2005 16:13:03 UTC) | |
DATE_ISO8601 | ISO-8601 (example: 2005-08-14T16:13:03+0000) | |
DATE_RFC822 | RFC 822 (example: Sun, 14 Aug 2005 16:13:03 UTC) | |
DATE_RFC850 | RFC 850 (example: Sunday, 14-Aug-05 16:13:03 UTC) | |
DATE_RFC1036 | RFC 1036 (example: Sunday, 14-Aug-05 16:13:03 UTC) | |
DATE_RFC1123 | RFC 1123 (example: Sun, 14 Aug 2005 16:13:03 UTC) | |
DATE_RFC2822 | RFC 2822 (Sun, 14 Aug 2005 16:13:03 +0000) | |
DATE_RSS | RSS (Sun, 14 Aug 2005 16:13:03 UTC) | |
DATE_W3C | World Wide Web Consortium (example: 2005-08-14T16:13:03+0000) |
其它相关网页:
虽然格式很简单, 但是有更多已经写好的 Library 可以直接使用, 会省掉不少时间, 详可参考:(不过下述我没用过就是了.. Orz.)
RSS Parser 可以考虑用下述的