您好我正在尝试计算两个日期之间的差异并且它工作正常但问题是即使1天高于下一个日期它也算作整月但是在我的代码中我得到8而不是9 这是我的代码
<?php
$date1 = '2016-09-17';
$date2 = date('Y-m-d');
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
echo $diff;
?>
答案 0 :(得分:0)
这将是可能的解决方案
<?php
$date_sale = '2016-09-17';
$date_now = date('Y-m-d');
$starting_date = new DateTime($date_sale);
$ending_date = new DateTime($date_now);
$interval_months = $ending_date->diff($starting_date);
$month_get = $interval_months->format('%m'); //get months;
$up_days = $interval_months->format('%d'); //get above days;
$show_final_months = 0;
if($up_days>0) {
echo $show_final_months = $month_get+1;
} else {
echo $show_final_months = $month_get;
}
?>