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;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM