PHP產(chǎn)生RSS pubDate所需日期時(shí)間格式

2011-11-29 10:57:47來源:作者:

RSS / ATOM 的格式要產(chǎn)生都很簡(jiǎn)單, 麻煩的是日期格式的產(chǎn)生(RSS => pubDate, ATOM => created).

RSS / ATOM 的格式要產(chǎn)生都很簡(jiǎn)單, 麻煩的是日期格式的產(chǎn)生(RSS => pubDate, ATOM => created).

發(fā)現(xiàn) PHP Date 有提供這些需要的相關(guān)全域變量可以使用:(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 預(yù)定義的日期格式

自 PHP 5.1.1 起定義有以下常量來提供標(biāo)準(zhǔn)日期表達(dá)方法,可以用于日期格式函數(shù)

  • 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

雖然格式簡(jiǎn)單, 但用PHP內(nèi)的預(yù)定義格式豈不更加方便.

目前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)  

其它相關(guān)網(wǎng)頁:

雖然格式很簡(jiǎn)單, 但是有更多已經(jīng)寫好的 Library 可以直接使用, 會(huì)省掉不少時(shí)間, 詳可參考:(不過下述我沒用過就是了.. Orz.)

RSS Parser 可以考慮用下述的
關(guān)鍵詞:PHPRSS日期