核心代码:
$sYear = substr($DateStart,4);
$eYear = substr($DateEnd,4);
$sMonth = substr($DateStart,2);
$eMonth = substr($DateEnd,2);
$sDay = substr($DateStart,2);
$eDay = substr($DateEnd,2);
$startTime = $this->fmDatetime($DateStart);
$endTime = $this->fmDatetime($DateEnd);
$dis = $endTime-$startTime;//得到俩个时间的秒数
$d = ceil($dis/(24*60*60));//得到天数
$rs['day'] = $d;//天数
$rs['hour'] = ceil($dis/(60*60));//小时
$rs['minute'] = ceil($dis/60);//分钟
$rs['second'] = $dis;//秒数
$rs['week'] = ceil($d/7);//周
$tem = ($eYear-$sYear)*12;//月份
$tem1 = $eYear-$sYear;//年
if($eMonth-$sMonth<0){//月份相减为负
$tem +=($eMonth-$sMonth);
}else if($eMonth==$sMonth){//月份相同
if($eDay-$sDay>=0){
$tem ++;
$tem1++;
}
}else if($eMonth-$sMonth>0){//月份相减正负
$tem1++;
if($eDay-$sDay>=0){//且日期相减为正数
$tem +=($eMonth-$sMonth)+1;
}else{
$tem +=($eMonth-$sMonth);
}
}
$rs['month'] = $tem;
$rs['year'] = $tem1;
return $rs;
}
}
一年多一天,返回的是2年,一个月多一天返回的是2个月,以此推......项目需要,才做此出来,开始我也到网上找这样的例子,但大家都是把年就按365天来算,月就按30天来算,这样算出来的结果肯定是没用的,年有可能是366天,月有可能是31,29,28都有可能