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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?