0

I'm currently pulling a RSS feed which has these dates:

<rss>
 <channel>
  <lastBuildDate>Thu, 18 Apr 2013 16:14:15 GMT</lastBuildDate>  

and

<item>
<pubDate>Fri, 05 Apr 2013 14:25:13 GMT</pubDate>  
</item>

<item>
<pubDate>Wed, 05 Sep 2012 10:01:27 GMT</pubDate>  
</item>

I am trying to work out the difference between the lastBuildDate and pubdate in days for every item.

So far I have this:

<?php
foreach($rss->channel->item as $item){

  $rss->channel->lastBuildDate = date('D, d M Y H:i:s GMT', strtotime($date1));
  $item->pubDate = date('D, d M Y H:i:s GMT', strtotime($date2));
  $dateDiff    = $date1 - $date2;   
  $fullDays    = floor($dateDiff/(60*60*24));   
  echo "Differernce is $fullDays days";

  ?>

Unfortunately each item is coming up with a 0 day difference. I know that $date1 and $date2 do not have a reference to the RSS feed, but considering the first half of the line does, does this still require a RSS path? Or am I pulling the RSS feed dates completely wrong in the first place?

Thanks in advance!

4

4 回答 4

1

Try this

  $dStart = new DateTime(date('2012-07-26'));
   $dEnd  = new DateTime(date('2012-08-26'));
   $dDiff = $dStart->diff($dEnd);
   echo $dDiff->format('%R');
   echo $dDiff->days;
于 2013-04-19T10:50:50.313 回答
0
  $date1 = 'Thu, 18 Apr 2013 16:14:15 GMT';
  $date2 = 'Wed, 05 Sep 2012 10:01:27 GMT';

  $dateDiff    = strtotime($date1) - strtotime($date2);   
  $fullDays    = floor($dateDiff/(60*60*24));   
  echo "Differernce is $fullDays days";
于 2013-04-19T10:49:45.270 回答
0

You should compare strtotime($date1) with strtotime($date2).

strtotime converts (nearly) any given date to a UnixTimestamp (http://en.wikipedia.org/wiki/Timestamp). UnixTimestamps count the seconds from the beginning of the unix era in seconds. If you compare these two you will get the difference in seconds wich can be outputted as days.

$diff = strtotime($date1) - strtotime($date2);
$fulldays = floor($diff/86400); //one day = 86400 seconds
于 2013-04-19T10:51:24.177 回答
0

you can do in this way by comparing two dates using strtotime function

$lastBuildDate = $rss->channel->lastBuildDate;
$pubDate = $item->pubDate;

$lb = strtotime($lastBuildDate);
$pd = strtotime($pubDate);

$differnce = $pd - $lb;
echo floor($differnce/(60*60*24)) . ' days difference';

this will output

212 days difference
于 2013-04-19T10:53:13.513 回答