【发布时间】:2012-07-26 09:59:16
【问题描述】:
我有一个函数可以计算两个日期之间的差异。
function getDateDifference($to, $from, $in) {
$diff = abs($to - $from);
$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));
if ($in == "days") {
return $days;
} else if ($in == "months") {
return $months;
} else if ($in == "years") {
return $years;
}
}
对于参数,我首先像这样将两个日期转换为秒,
checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);
当涉及不到一个月的差异时,我得到了正确的差异。但是如果差值超过一个月,我总是得到差值 1。有人可以告诉我问题是什么。
【问题讨论】: