2014-07-22 56 views
0

我試圖設置一個php腳本來查看XML,如果某個項目的pubDate超過7天,它會刪除整個項目。我想出迄今:從基於XML的日期中刪除項目

<?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"; 
    } 


} 


?> 

這給了我50名相呼應,只有其中1個是刪除,而XML卻只有房子10個項目,其中只有一個應該保持。你可以給我一些指導,讓這個設置正確,然後也可以告訴我如何刪除該項目,一旦我已經正確識別它們?

回答

0

您將值作爲RSS日期格式的字符串進行比較。這與Mon, 15 Aug 2005 15:52:01 +0000類似,例如Fri將會小於Mon。我建議你直接比較strtotime()的結果。

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

另一種方式是使用XPath有回調到PHP:https://stackoverflow.com/a/23683347/2265374

+0

我想你貼什麼,我得到的回報是'不deleteDon't deleteDon't deleteDon't deleteDon't deleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDeleteDelete' 在這一點上,只有8項,所以應該在沒有辦法了,許多相呼應。 似乎我在循環中的限制可能會搞砸,因爲它迫使它總是返回50個回聲。 – user717452

+0

我將我的for循環更改爲'for($ i = 0; $ i user717452