2008年3月7日 星期五

PHP中RSS的RFC 822日期格式轉換

原來在PHP中轉換日期格式成 RFC 822是這麼容易的事情
先前在程式中轉換時間格式的寫法
$strPubDate = Date('D, d M Y H:i:s O',$intDate);
剛剛在看PHP線上手冊時
突然看到Date函數下方的說明時看到了ChangeLog
之前"目小"都沒沒看到ChangeLog
5.1.1 There are useful constants of standard date/time formats that can be used to specify the format parameter.
原來PHP開發團隊早在PHP 5.1.1版時就已經預先加入一些常用的日期格式常數
方便開發者進行日期格式轉換
http://tw.php.net/manual/en/ref.datetime.php#datetime.constants
節錄其中的一些說明
DATE_ATOM (string)
Atom (example: 2005-08-15T15:52:01+00:00)
DATE_COOKIE (string)
HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC)
DATE_ISO8601 (string)
ISO-8601 (example: 2005-08-15T15:52:01+0000)
DATE_RFC822 (string)
RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000)
DATE_RFC850 (string)
RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC)
DATE_RFC1036 (string)
RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000)
DATE_RFC1123 (string)
RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000)
DATE_RFC2822 (string)
RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000)
DATE_RFC3339 (string)
Same as DATE_ATOM (since PHP 5.1.3)
DATE_RSS (string)
RSS (Mon, 15 Aug 2005 15:52:01 +0000)
DATE_W3C (string)
World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00)

所以現在作日期格式轉換時可以改寫的成
$strPubDate = Date(DATE_RSS,$intDate);
$strPubDate = Date(DATE_RFC2822,$intDate);
方便又有效率
完整的RSS格式可參考下面網址
http://cyber.law.harvard.edu/rss/rss.html

沒有留言: