44

I'm having trouble getting the date of my RSS Feed to run correctly. Do you know what the proper date to show it is?

I have it stored in a field called creation_date in this format: 2012-08-14 10:17:12

Then i grab it:

$pubDate = $article[creation_date];

Then I convert it:

$pubDate= date("Y-m-d", strtotime($pubDate));

Then within my item tag I place it:

<pubdate>'.date("l, F d, Y", strtotime($pubDate)).'</pubdate>

Is there something that I'm not seeing?

splash
  • 13,037
  • 1
  • 44
  • 67
Thingamajig
  • 4,107
  • 7
  • 33
  • 61
  • possible duplicate of [How to properly place Date in element on RSS feed](http://stackoverflow.com/questions/9405309/how-to-properly-place-date-in-pubdate-element-on-rss-feed) – random Aug 19 '12 at 06:51

10 Answers10

73

The PHP date function has already a way to format pubDate (RFC 2822) compliant dates:

date('r', $timestamp);
Nebel54
  • 1,308
  • 1
  • 10
  • 10
30

Solved:

$pubDate = $article[creation_date]; 
$pubDate= date("D, d M Y H:i:s T", strtotime($pubDate));

then in my echo'd code:

 <pubDate>'.$pubDate.'</pubDate>
Thingamajig
  • 4,107
  • 7
  • 33
  • 61
22

See pubDate definition in RSS 2.0 Specification:

All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).

Here are 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>

See also Problematical RFC 822 date-time value.

splash
  • 13,037
  • 1
  • 44
  • 67
12

Use this format: D, d M Y H:i:s O. See http://php.net/manual/en/class.datetime.php

Or use DateTime constants for more easy usage: DateTime::RSS

Edd
  • 683
  • 1
  • 11
  • 21
11

Rss pubDate uses the RFC 2822 standards. You can achieve this in php by invoking the r argument on the date function, i.e:

<?php
$pubDate= date('r', time());
echo "<pubDate>$pubDate</pubDate>";
# <pubDate>Thu, 20 Dec 2022 02:46:11 UTC</pubDate>
?>

If you prefer the DateTime class, use:

$pubDate = new DateTime();
echo $pubDate->format(DateTime::RSS);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
5

What about DateTime object (PHP 5 >= 5.2.0)

\DateTime::createFromFormat(\DateTime::RSS, $RSSDate); // converting RSS date to object

or

date(\DateTime::RSS, $timestamp); // formatting timestamp to RSS time

or both

$dto = \DateTime::createFromFormat(\DateTime::RSS, $RSSDate);
date('d-M-Y H:i:s', $dto->getTimestamp()); // formatting RSS date to anything you want

or even better

$dto = \DateTime::createFromFormat(\DateTime::RSS, $RSSDate);
$formattedDate = $dto->format('d-M-Y H:i:s');
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
5

While the accepted answer ("D, d M Y H:i:s T") works as expected most of the time, it is not 100% correct. In multilingual situations this string may give non English text which won't be accepted as RFC compliant. To be always sure that the English version is used, use "r".

A.L
  • 10,259
  • 10
  • 67
  • 98
4

The easiest method is to use the DATE_RSS predefined constant (available since PHP 5.1.0).

$pubDate = date(DATE_RSS, strtotime($pubDate));
weszy
  • 41
  • 1
1

I have used like this:

$item->date = date('D, d M Y H:i:s GMT', strtotime($myBlogPublishedTime));

my hp rss sample

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
matao
  • 11
  • 1
0

This is how I solved to compile a valid RFC-822 datetime for RSS into XSLT:

<xsl:variable name="now" select="fn:current-dateTime()"/>
<xsl:value-of select="format-dateTime($now, '[FNn,3-3], [D01] [MNn,3-3] [Y0001] [H01]:[m01]:[s01] [Z0001]')"/>