2 つの日付の差を計算する関数があります。
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;
}
}
パラメータについては、最初に2つの日付を次のように秒に変換します。
checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);
1か月未満の差に関しては、正しい差が得られます。しかし、差が1か月以上ある場合、私は常に差を1として取得しています。誰かが問題を教えてもらえますか。