The date and time format required for generating RSS pubDate using PHP

Source: Internet
Author: User
Tags echo date rfc

The SS/ATOM format is easy to generate. The trouble is that the date format is generated (RSS => pubDate, ATOM => created ).

We found that PHP Date provides the required global variables: (PHP 5.1.1 will support these global variables later)

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

For MySQL timestamp/datetime format, you can directly use:

Date (DATE_RSS, ($ MYSQL_DATETIME_DATA ));

Currently, the following date global variables are supported:

DATE_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 consor.pdf (example: 15t15: 52: 01 + 00: 00)

PHP predefined date format

The following constants are defined from PHP 5.1.1 to provide standard date expression methods. They can be used for date format functions.

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

Although the format is simple, it is not more convenient to use the predefined format in PHP.

Currently, PHP supports the following date formats:

DATE_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 consor.pdf (example: 15t15: 52: 01 + 00: 00)

 

Generate rss example

Header ("Content-type: application/xml"); // defines the header to prevent browsers in the chrome kernel from supporting rss subscription, if not, output an xml file/*** to query the database and obtain the data to output rss */$ database = "nameofthedatabase"; $ dbconnect = mysql_pconnect (localhost, dbuser, dbpassword); mysql_select_db ($ database, $ dbconnect); $ query = "select link, headline, description from 'headlines' limit 15"; $ result = mysql_query ($ query, $ dbconnect); while ($ line = mysql_fetch_assoc ($ resu Lt) {$ return [] = $ line;} $ now = date ("D, d m y h: I: s T"); $ output = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Rss version = \ "2.0 \"> <channel> <title> Shandong Road traffic safety network-$ lanmu </title> <link> http://sdjtaq.cn/rss </link> <description> Latest 100 articles of $ lanmu </description> <pubDate> $ now </pubDate> <lastBuildDate> $ now </lastBuildDate> <docs> http://someurl.com </docs> <managingEditor> 810706080@qq.com </ managingEditor> <webMaster> 810706080@qq.com </webMaster> "; foreach ($ artList as $ line) {$ date = strtotime ($ line ['add _ ti Me ']); // format the database time to the timestamp $ output. = "<item> \ n ". "<title> ". $ line ['title']. "</title> \ n ". "<link> http://sdjtaq.cn/aq_show ". $ line ['id']. ". html </link> \ n ". "<description> <! [CDATA [". $ line ['zhaiao']. "]> </description> \ n ". "<pubDate> ". date ("D, d m y h: I: s O", $ date ). "</pubDate> \ n ". "</item> \ n" ;}$ output. = "</channel> \ n </rss>"; echo $ output;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.