function getMonthNum( $start_time, $end_time){
$date1 = explode('-',$start_time);
$date2 = explode('-',$end_time);
if($date1[0] == $date2[0]){
if($date1[1] == $date2[1]){
$month = 0;
$day = $date2[2]-$date1[2];
}else{
$month = $date2[1]-$date1[1];
if($date1[2]>$date2[2]){
$day = date('t',strtotime($start_time))-($date1[2]-$date2[2]);
$month-=1;
}else{
$day = $date2[2]-$date1[2];
}
}
}else{
$month = 12;
if($date1[1] == $date2[1]){
if($date1[2]>$date2[2]){
$day = date('t',strtotime($start_time))-($date1[2]-$date2[2]);
$month-=1;
}else{
$day = $date2[2]-$date1[2];
}
}else{
if($date1[1]>$date2[1]){
$month -= $date1[1]-$date2[1];
if($date1[2]>$date2[2]){
$day = date('t',strtotime($start_time))-($date1[2]-$date2[2]);
$month-=1;
}else{
$day = $date2[2]-$date1[2];
}
}else{
$month += $date2[1]-$date1[1];
if($date1[2]>$date2[2]){
$day = date('t',strtotime($start_time))-($date1[2]-$date2[2]);
$month-=1;
}else{
$day = $date2[2]-$date1[2];
}
}
}
}
return [
'start_time'=>$start_time,
'end_time'=>$end_time,
'month'=>$month,
'day'=>$day,
];
}
调用getMonthNum
$this->getMonthNum('2022-02-04','2023-03-05');
输出 month---- 1 day---- 1
|