drh96824 2013-12-18 21:07
浏览 62
已采纳

在php中按pubdate排序rss

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

  • 写回答

1条回答 默认 最新

  • dotwc62080 2013-12-18 21:40
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料