Zend RSS feed pubDate时间格式

时间:2011-02-24 18:08:28

标签: zend-framework datetime formatting rss

希望对于知识渊博的人来说这是一个快速的问题,但是我在尝试在Zend框架项目中获取RSS源的正确日期时间格式时遇到了问题,并且耗尽了所有可用的资源; php datetime我无法完全理解的唯一资源是Zend文档,因为我不太确定如何正确调用它们引用的静态类Zend Date_time。 DB字段也是日期时间类型......

除了将pubDate输出为XML格式之外,我已将所有数据都添加到Feed中。由于格式无法正确识别,因此Feed只会吐出每个帖子的当前日期。只要我得到了... ...

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();
}

我已经尝试了所有其他方法来获得正确的格式,包括;

 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));

任何指针都会很棒!

2 个答案:

答案 0 :(得分:4)

pubDate或published是一个可选字段,不是必需的。所以我会对此发表评论。

我会添加'lastUpdate'=> strtotime($ article-> publishDate)到$ feedArray。请注意,Zend RSS编译器将对此进行gmdate格式化。

而不是使用import Array,请使用importBuilder。

有关任何额外信息,请参阅:http://framework.zend.com/manual/en/zend.feed.importing.html

最佳做法是仅显示最新的Feed(在这种情况下,我已经完成了10次)

所以代码应该是:

    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();
}

试一试。

答案 1 :(得分:1)

您需要将日期输出为RFC_2822格式。 试试这个:

$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)
    );       
}