两个可变RSS日期(以天为单位)之间的差值


Difference between 2 variable RSS dates in days

我目前正在提取一个RSS提要,其中包含以下日期:

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

<item>
<pubDate>Fri, 05 Apr 2013 14:25:13 GMT</pubDate>  
</item>
<item>
<pubDate>Wed, 05 Sep 2012 10:01:27 GMT</pubDate>  
</item>

我正试图找出lastBuildDate和publdate在每一个项目的天之间的差异。

到目前为止,我有这个:

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

不幸的是,每个项目都有0天的差异。我知道$date1和$date2没有对RSS提要的引用,但是考虑到该行的前半部分有,这还需要RSS路径吗?还是我一开始就把RSS订阅日期搞错了?

提前感谢!

试试这个

  $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;

你应该比较strtotime($date1)strtotime($date2)

strtotime将(几乎)任何给定的日期转换为UnixTimestamp (http://en.wikipedia.org/wiki/Timestamp)。UnixTimestamps以秒为单位计算从unix纪元开始的秒数。如果你比较这两个,你会得到秒的差异,可以输出为天。

$diff = strtotime($date1) - strtotime($date2);
$fulldays = floor($diff/86400); //one day = 86400 seconds
  $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";

您可以使用strtotime函数比较两个日期

$lastBuildDate = $rss->channel->lastBuildDate;
$pubDate = $item->pubDate;
$lb = strtotime($lastBuildDate);
$pd = strtotime($pubDate);
$differnce = $pd - $lb;
echo floor($differnce/(60*60*24)) . ' days difference';

这将输出

212 days difference