sinewave
sinewave

Reputation: 137

Zend RSS feed pubDate time formatting

Hopefully this is a quick one for someone in the knowledge but i am having a problem trying to get the correct datetime format for an RSS feed in a Zend framework project and exhasuted all avaliable resources; php datetime The only resource I cant fully comprehend is the Zend documentation as Im not quite sure how to correctly call the static class they refer too Zend Date_time. Also the DB field is a datetime type...

I have got as far as getting all my data into the feed with the exception of the pubDate out putting in XML format. As the format is not recognised correctly the feed is just spitting out the current date for each post. Heres as far as I got...

public function rssAction()
{

    $this->_helper->layout->setLayout('xmllayout');

    $model = new Default_Model_News;
$newsitems = $model->fetchAll();
    $date = date("D\, j M Y G:i:s");

    $feedArray = array (
            'title'            =>    "Postgoldforcash News Feed",
            'description'      =>    "Postgoldforcash News Feed",
            'link'             =>    "http://www.postgoldforcash.com",
            'language'         =>    'en-EN',
            'charset'          =>    'utf-8',
            'docs'             =>    "Postgoldforcash News",
            'pubDate'          =>     $date,
            'entries'          =>    array()
        );

    foreach ( $newsitems as $article ) {
    $fDate = date_format(new DateTime($article->publishDate), "r");
        $feedArray['entries'][] = array (
                    'title'           =>    $article->title,
                    'link'            =>    $article->url."/", // for some reason i have to add a blank space or '/' in otherwise it breaks...
                    'guid'            =>    $article->title,
                    'description'     =>    $article->content,
                    'pubDate'         =>    $fDate
            );       
    }
    $feed = Zend_Feed::importArray($feedArray, 'rss');
    $feed->send();
}

I have tried all the other ways of getting the correct formatting including;

 date_format(new DateTime($article->publishDate), "D\, j M Y G:i:s");


strftime ($article->publishDate, "%a, %d %b %Y %H:%M:%S %z") ;


gmdate(DATE_RSS, strtotime($article->publishDate));

Any pointers would be great!

Upvotes: 3

Views: 2256

Answers (2)

Nicolas Marengo
Nicolas Marengo

Reputation: 309

pubDate or published is an optional field and its not required. So i would comment that out.

I would add 'lastUpdate' => strtotime($article->publishDate) to the $feedArray. Note that the Zend RSS compiler will then do gmdate to this to format it.

And instead of using import Array, use importBuilder.

Please refer to: http://framework.zend.com/manual/en/zend.feed.importing.html for any extra information.

Its also best practice to only display the latest feeds (in this case ive done 10)

So the code should read:

    public function rssAction() {

    $this->_helper->layout->setLayout('xmllayout');

    $model = new Default_Model_News;
    $newsitems = $model->fetchAll();
    $date = date("YYYY-MM-dd HH:mm:ss");

    $feedArray = array(
        'title' => "Postgoldforcash News Feed",
        'description' => "Postgoldforcash News Feed",
        'link' => "http://www.postgoldforcash.com",
        'language' => 'en-EN',
        'charset' => 'utf-8',
        'docs' => "Postgoldforcash News",
        'generator' => 'Zend Framework Zend_Feed',
        'entries' => array()
    );

    $i = 0;
    foreach ($newsitems as $article) {
        $i++;
        if ($i > 10)
            break;
        $feedArray['entries'][] = array(
            'title' => html_entity_decode($article->title),
            'link' => $article->url ."/",
            'guid' => $article->url,
            'description' => strip_tags($article->content),
            'lastUpdate' => strtotime($article->publishDate)

        );
    }
    $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($feedArray), 'rss');
    $feed->send();
}

Give it a go.

Upvotes: 4

Maxime P
Maxime P

Reputation: 775

You need to output the date to the RFC_2822 format. Try that:

$fDate = new Zend_Date();
foreach ( $newsitems as $article ) {
    $date->set($article->publishDate, 'YYYY-MM-dd HH:mm:ss');
    $feedArray['entries'][] = array (
        'title'         => $article->title,
        'link'          => $article->url."/", // for some reason i have to add a blank space or '/' in otherwise it breaks...
        'guid'          => $article->title,
        'description'   => $article->content,
        'pubDate'       => $date->get(Zend_Date::RFC_2822)
    );       
}

Upvotes: 1

Related Questions