计算两个时间之间相隔几个月
方法如下:
function getMonthNum($date1, $date2, $tags = '-')
{
$date1 = explode($tags, $date1);
$date2 = explode($tags, $date2);
$year = $date1[0] - $date2[0];
$month = $date1[1] - $date2[1];
if ($year > 0) {
if ($month < 0) {
return abs($year) * 12 - abs($month);
}
return abs($year) * 12 + abs($month);
} elseif ($year < 0) {
if ($month > 0) {
return abs($year) * 12 - abs($month);
}
return abs($year) * 12 + abs($month);
} else {
if ($month > 0) {
return $month;
}
return abs($month);
}
}
$_monthCount = getMonthNum('2016-1', '2017-6');
如有问题请留言给我哦,谢谢!