php 获取时间相差的月份和天数

function getMonthAndDay($date1,$date2){
$datestart= date('Y-m-d',strtotime($date1));
if(strtotime($datestart)>strtotime($date2)){
$tmp=$date2;
$date2=$datestart;
$datestart=$tmp;
}
list($Y1,$m1,$d1)=explode('-',$datestart);
list($Y2,$m2,$d2)=explode('-',$date2);
$Y=$Y2-$Y1;
$m=$m2-$m1;
$d=$d2-$d1;
if($d<0){
$d+=(int)date('t',strtotime("-1 month $date2"));
$m--;
}
if($m<0){
$m+=12;
$Y--;
}

$res['month'] = 0;
if($Y == 0){
$res['month'] = $m;
$res['day'] = $d;
return $res;
}elseif($Y == 0 && $m == 0){
$res['day'] = $d;
return $res;
}else{
$res['month'] = $m+$Y*12;
$res['day'] = $d;
return $res;
}
}
原文地址:https://www.cnblogs.com/phpwyl/p/12092840.html