Search code examples
phpvalidationrssw3cpubdate

Convert int-date to RSS PubDate RFC 822 PHP


I have a question that is making me crazy, My Task is to parse a date from an API and transform it to RFC 822 format, because the feed that is coming out gets an validation error

the date from the API looks like this :

<review created="2012-10-23 14:51:12.0">

I have one Date in the description made via substr

$xtime = substr($review["created"], 0, 16);
$jahr = substr($xtime,0,4);
$mon  = substr($xtime,5,2);
$tag  = substr($xtime,8,2);
$datneu = $tag.'.'.$mon.'.'.$jahr; 

this date will be rendered like :

23.10.2012

For the pubdate I made

$xtime = substr($review["created"], 0, 16);
$xxl = $pubtime . " GMT";

rendered like :

2012-10-23 14:51:12 GMT

And W3C feed validator says it´s not validate because pubDate is not in RFC 822 form

Sorry

This feed does not validate.

line 10, column 38: pubDate must be an RFC-822 date-time: 2012-10-29 11:51:23 GMT (5 occurrences) [help]

          <pubDate>2012-10-29 11:51:23 GMT</pubDate>

and it needs to look like :

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

i can imagine a hacky solution for expressing sth like "if (month = 01){ actualmonth = Jan}" but i really don´t know how to do same with the days,

Also i´m not too comfortable with PHP but I need to solve this asap.

Hope you can help me, there must be a solution i didnt found at similiar questions

regards John


Solution

  • Have a look at DateTime::createFromFormat() or date_create_from_format.

    https://www.php.net/manual/en/datetime.createfromformat.php

    <?php
    $date = date_create_from_format('Y-m-d H:i:s', '2012-10-23 14:51:12.0');
    echo date_format($date, 'D, d M Y H:i:s');
    ?>
    

    Have a look at the possible date formats

    https://www.php.net/manual/en/function.date.php

    EDIT: fixed