pubDate以来的时间

时间:2012-05-09 12:07:48

标签: php time rss

在一个新项目中,我正在使用由PHP读取并显示在页面上的rss。 我想做的一件事是显示自帖子发布以来已经过了多少时间,但是我找不到办法这样做,这是我目前的代码,希望有人可以帮助我!

echo "<div id=\"left\">";
    $rss1 = new DOMDocument();
    $rss1->load('http://www.macfan.nl/macfan.rss');
    echo '<h2>MacFan</h2>';
    $feed = array();
    foreach ($rss1->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = 5;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $date = date('F d', strtotime($feed[$x]['date']));
        echo '<p><a href="'.$link.'" title="'.$title.'">'.$title.'</a></p>';
        echo "<p class=\"small\">$date</p>";
    }
echo "</div>";

1 个答案:

答案 0 :(得分:1)

您可以比较unix时间戳;

$seconds_between_now_and_then=(time()-strtotime($feed[$x]['date']));

然后你可以看到它有多远。以下这些可以帮助您使自己更具可读性:

$minutes_between_now_and_then=$seconds_between_now_and_then/60;
$hours_between_now_and_then=$minutes_between_now_and_then/60;
$days_between_now_and_then=$minutes_between_now_and_then/24;

echo 'Seconds:'.$seconds_between_now_and_then.'<br />';
echo 'Minutes:'.$minutes_between_now_and_then.'<br />';
echo 'Hours:'.$hours_between_now_and_then.'<br />';
echo 'Days:'.$days_between_now_and_then;