Php How can I get the date of the oldest item in an rss feed?

using simplexml to parse the feed, but I want to take the date of the oldest item in the feed. Does anyone know how to do this? Thanks to

$ rss = simplexml_load_file (" http://search.twitter.com/search.atom?lang=en&q=foobar&rpp=100&page=1 ");

+2


source to share


2 answers


If you just want to get the last element <entry>

(in the document-order ), you can use SimpleXMLElement :: xpath () and the last () function
http://www.w3.org/TR/xpath/ says:

child :: para [position () = last ()] selects the last child from the context node

eg.



$url = "http://search.twitter.com/search.atom?lang=en&q=foobar&rpp=100&page=1"; 
$feed = simplexml_load_file($url);
$feed->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');

$entry = $feed->xpath('//atom:entry[position()=last()]');
if ( isset($entry[0]) ) {
  $entry = $entry[0];
}
else {
  die('not found');
}
var_dump($entry);

      

[position()=last()]

can be reduced to [last()]

If the oldest entry is not the last entry in document-order, you need something else.

0


source


  • First, download all the items from the RSS feed and store them in an array like this .

  • Now sort the array by date .

  • Get the first / last result depending on your sort order.



+1


source







All Articles