PHP怎样计算两个日期相差几个月?
<?php
function getMonthNum( $date1, $date2, $tags='-' ){
$date1 = explode($tags,$date1);
$date2 = explode($tags,$date2);
return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);
}
echo getMonthNum("2013-02-01","2014-01-01",'-');
?>
function getMonthNum($date1,$date2){
$date1_stamp=strtotime($date1);
$date2_stamp=strtotime($date2);
list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
}
echo getMonthNum("2013-02-01","2014-01-01");
echo getMonthNum("20130201","20140101");
echo getMonthNum("201302","201401");
<?php
function getMonthNum( $date1, $date2, $tags='-' ){
$date1 = explode($tags,$date1);
$date2 = explode($tags,$date2);
if ($date1[0]>=$date2[0]){
return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);
}
else{
return abs($date2[0] - $date1[0]) * 12 - abs($date1[1] - $date2[1]);
}
}
echo getMonthNum("2013-02-01","2014-01-01",'-');
?>
function getMonthNum($date1,$date2){
$date1_stamp=strtotime($date1);
$date2_stamp=strtotime($date2);
list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
return abs(($date_2['y']-$date_1['y'])*12 +$date_2['m']-$date_1['m']);
}
echo getMonthNum("2013-02-01","2014-01-01");
echo getMonthNum("20130201","20140101");
echo getMonthNum("201302","201401");