9

I'm using RSS Graffitty to post RSS items to a facebook page.

The app told me the items were missing the publication date so I added this tag:

echo "<pubdate>".$row['Date']."</pubdate>";

$row['Date'] is obtained from my MySQL database and it's a Datetime column.

How must I format it/echo it so it's recognized by the RSS feed? Must I change the element?

Thanks

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

2 Answers2

14

RSS 2.0 specifications on the <pubDate> element should conform to the RFC 822 Date and Time syntax. Namely, to display it in the following format:

Fri, 21 Dec 2012 10:00:01 GMT

If you error run your RSS feed through the W3C Feed Validator you'll note these examples of valid RFC822 date-times:

<pubDate>Wed, 02 Oct 2002 08:00:00 EST</pubDate>

<pubDate>Wed, 02 Oct 2002 13:00:00 GMT</pubDate>

<pubDate>Wed, 02 Oct 2002 15:00:00 +0200</pubDate>

If you wanted to use MySQL formatting, you'd call the column with the following use of date_format(), adjusting the UTC modifier/marker as necessary:

date_format(Date, '%a, %d %b %Y %H:%i:%s')

Or you can do it via the PHP date method with DATE_RSS setting the format for you:

echo "<pubdate>".date(DATE_RSS, strtotime($row['Date']))."</pubdate>";

Make sure your <pubDate> element appear within its parent <item> node.

Also note that this element is case-sensitive. You must output pubDate with the capital D and not all lowercase (pubdate) as other elements can.

random
  • 9,774
  • 10
  • 66
  • 83
  • The feedvalidator tells me: `Fri, 24 Feb 12 18:11:13 -0700` is wrong. It seems compliant with the example. Do you have any idea why it could be happening? http://feed2.w3.org/check.cgi?url=http%3A%2F%2Flujanventas.com – lisovaccaro Feb 25 '12 at 03:53
  • Make the year as four digits and that should fix it all up @lis – random Feb 25 '12 at 04:01
  • It is probably better to use `DATE_RSS` instead of `DATE_RFC822` since in the dates generated by the latter, years are represented by only two digits. – Percival Ulysses Nov 08 '13 at 01:02
1

Just a hint: the -r switch in the php date function is the shorthand for the RFC 2822 format, which actually does yield a four digit year.

See PHP Doc for reference.

DukeDrake
  • 156
  • 2
  • 4