Johan
Johan

Reputation: 211

sort rss feed by pubdate in php

I am trying to sort my RSS feed by pubdate, but untill now unsuccesfully i have tried usort method but i cant get the code to work

My actual rss page link click here

and here is my php code to display a feed page

    <?php
$rssfeed = "<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
        <title>My RSS feed</title>
        <link>http://" . $_SERVER['HTTP_HOST'] . "/</link>
        <description>This is an example RSS feed</description>
        <language>en-us</language>
        <copyright>Copyright (C) 2009 mywebsite.com</copyright>
";
$links = scandir('pages/');
$links = array_diff($links, array('.', '..', 'subpages', 'protected'));
foreach($links as $link){
$descr = file_get_contents('description/' . $link);
$descr = str_replace(array('\\'), array(''), $descr);   
$pub = date ('Y m d Hi:s', filemtime('pages/'.$link));
    $rssfeed .= "<item>
            <title>".$link."</title>
<description>".$descr."</description>
<link>http://" . $_SERVER['HTTP_HOST'] . "/index.php?p=".$link."</link>
            <pubDate>".$pub."</pubDate>
                </item>";
}

$links = scandir('pages/subpages/');
$links = array_diff($links, array('.', '..'));
foreach($links as $link){
$descr = file_get_contents('description/' . $link);
$descr = str_replace(array('\\'), array(''), $descr);   
$pub = date ('Y m d Hi:s', filemtime('pages/subpages/'.$link));
    $rssfeed .= "<item>
            <title>".$link."</title>
<description>".$descr."</description>
<link>http://" . $_SERVER['HTTP_HOST'] . "/index.php?p=".$link."</link>
            <pubDate>".$pub."</pubDate>
                </item>";
}
    $rssfeed .= "</channel></rss>";
echo $rssfeed;
?>

i tried before with usort somthing like

function cmp($pub, $b)
{
    if ($pub == $b) {
        return 0;
    }
    return ($pub < $b) ? -1 : 1;
}
usort($a, "cmp");

vut i cant get it to work. Any thoughts? all help is much appreciated

Upvotes: 0

Views: 780

Answers (1)

zerzer
zerzer

Reputation: 602

this can be performed using date_diff() function of PHP check it here http://php.net/manual/en/datetime.diff.php

Upvotes: 1

Related Questions