-1

我正在使用 PHP 向我的网站调用 RSS 提要。目前,我下面的代码正在调用 pubDate 的全部内容:

<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>

如何仅显示上述示例中的日期和月份,即 9 月 12 日?

编辑

我应该澄清一下,上面的代码行是我目前得到的一个示例输出,但是当我从 RSS 提要中调用最新的 3 篇文章时,这个日期和时间会有所不同。因此,我需要代码更加动态(如果这是正确的术语!)

这段代码是我获取 RSS 提要内容的完整代码:

<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
    $title = (string) $item->title; // Title Post
    $link   = (string) $item->link; // Url Link
    $pubDate   = (string) $item->pubDate; // date
    $description = (string) $item->description; //Description Post

    echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
    echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';

    if($counter == 2 ) {
        break;
    } else {
        $counter++;
    }

} ?>
4

4 回答 4

1

使用strtotimedate

$pubDate = 'Thu, 12 Sep 2013 07:23:59 +0000';

$pubDate = date('j M', strtotime($pubDate)); //This is the only one you need!

var_dump($pubDate); //string(6) "12 Sep"
于 2013-11-04T09:30:38.210 回答
0

您可以使用date_parse解析日期,month然后day在结果数组中使用 和 的值。

于 2013-11-04T09:32:09.830 回答
0

即使这样也有效

<?php
$str="<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>";
$str=explode(" ",$str);
echo $str[1]." ".$str[2];//12 Sep

编辑:

<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
    $title = (string) $item->title; // Title Post
    $link   = (string) $item->link; // Url Link
    $pubDate   = (string) $item->pubDate; // date
    $pubDate=explode(" ",$pubDate);
    $pubDate =  $pubDate[1]." ".$pubDate[2];
    $description = (string) $item->description; //Description Post

    echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
    echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';

    if($counter == 2 ) {
        break;
    } else {
        $counter++;
    }

} ?>
于 2013-11-04T09:32:16.567 回答
0

您可以使用带有所需正则表达式的 preg_match() 函数来获取特定数据。例如

$content="2013 年 9 月 12 日星期四 07:23:59 +0000";

preg_match("/.*,(.*)20[0-9][0-9]/","$content",$g_val) ;

$g_val[1] 将有“ 12 Sep”

于 2013-11-04T09:37:58.843 回答