Reputation: 153
In a new project, I'm working with rss being read by PHP and displayed on a page. One thing I'd like to do is show how much time has passed since the post was published, but I can't find a way to do so, this is my current code, hope somebody can help me!
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(' & ', ' & ', $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>";
Upvotes: 0
Views: 181
Reputation: 1070
You can compare the unix timestamps;
$seconds_between_now_and_then=(time()-strtotime($feed[$x]['date']));
Then you can see how far it is apart. These below could help you make it more readable for yourself:
$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;
Upvotes: 1