如题:
例如:2008-10-10 至 2010-10-23 之间的月数
是用于计算年龄的,现在是用这个来计算生日到现在的月数,$birthday=(date("Y")-$date_elements[0]) * 12 + date("m") - $date_elements[1] + round((date("d") - $date_elements[2])/30); 希望能按照这种来算出,不然和之前的就不准了,谢谢!
例如:2008-10-10 至 2010-10-23 之间的月数
是用于计算年龄的,现在是用这个来计算生日到现在的月数,$birthday=(date("Y")-$date_elements[0]) * 12 + date("m") - $date_elements[1] + round((date("d") - $date_elements[2])/30); 希望能按照这种来算出,不然和之前的就不准了,谢谢!
代码如下即可:
$d1=new DateTime('2008-10-10');
$d2=new DateTime('2010-10-23');
echo '两者之间的月数之差为'.((date_diff($d1,$d2)->y)*12+(date_diff($d1,$d2)->m)).'个月';
function date_diff($date1, $date2) {
$current = $date1;
$datetime2 = date_create($date2);
$count = 0;
while(date_create($current) < $datetime2){
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
$count++;
}
return $count;
}
$current = $date1;
$datetime2 = date_create(strval($date2));
$count = 0;
while(date_create($current) < $datetime2){
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
$count++;
}
return $count;
}