使用下面的PHP代码,我希望得到'2'作为我的输出。但我得到'1'。
有谁知道这是为什么?
$returndate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-28');
$departdate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-26');
$diff = abs(strtotime($returndate) - strtotime($departdate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
echo $days; // expecting 2, but get 1
非常感谢您的帮助。
答案 0 :(得分:5)
这么多的计算......我假设你有一个四舍五入的问题,所有的时间测量都在四舍五入......这里有一个更简单的看看你在做什么:
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
答案 1 :(得分:4)
$d1 = new DateTime('2011-03-28');
$d2 = new DateTime('2011-03-26');
echo $d1->diff($d2)->d;
输出:2