douchi8503 2014-07-22 14:38
浏览 16
已采纳

从基于XML的关闭日期中删除项目

I am trying to set up a php script that will look through an XML, and if an item has a pubDate of more than 7 days old, it deletes the entire item. What I have come up with so far is:

<?php

$rss = new DOMDocument();
$url = 'http://www.url.net/app/Test.xml';
$rss->load($url);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('date')->item(0)->nodeValue,
        );
    array_push($feed, $item);

}

$limit = 50;
for($x=0;$x<$limit;$x++) {
   date_default_timezone_set('America/Los_Angeles');
$newDate = date("D, d M Y G:i:s -0400", strtotime("-8 day"));
    $date = date('D, d M Y G:i:s -0400', strtotime($feed[$x]['date']));
        $theitem = date('D, d M Y G:i:s -0400', strtotime($feed[$x]['date']));
 if ($date > $newDate) {
        echo "Don't delete";
    }

    else {
        echo "Delete";
    }


}


?>

This gives me 50 echoes, only 1 of which is Delete, while the XML actually only houses 10 items, and only one of them should remain. Can you give me some guidance for getting this set up right, and then also on how to remove the item, once I have identified them properly?

  • 写回答

1条回答 默认 最新

  • dongzhan8620 2014-07-22 16:21
    关注

    You're comparing the values as strings in the RSS date format. This is something like Mon, 15 Aug 2005 15:52:01 +0000 and for example Fri will be smaller then Mon. I suggest you compare the result of strtotime() directly.

    $newDate = strtotime("-8 day");
    $date = strtotime($feed[$x]['date']);
    if ($date > $newDate) {
         echo "Don't delete";
    } else {
      echo "Delete";
    }
    

    Another way is using Xpath with a callback into PHP: https://stackoverflow.com/a/23683347/2265374

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

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)