我正在尝试使用此代码区分日期,但它无法产生正确的结果,此代码有什么问题,我找不到。
$birth_date = new DateTime("1977-03-23");
$current_date = new DateTime();
$diff = $birth_date->diff($current_date);
echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
echo $days = $diff->days . " days"; echo "<br/>";
显示Outout为 - 39年10个月16天 但我认为产量应该是 - 39年10个月7天
它正确计算年份和月份,但天数错误。
答案 0 :(得分:0)
也许这个功能可以帮到你:
function leap_year($date1, $date2)
{
$y1 = $date1->format('Y');
if ($date1->format('m')>2) $y1++;
$y2 = $date2->format('Y');
if ($date2->format('m')<=2) $y2--;
$leap_years = 0;
for($i = $y1; $i <= $y2; $i++)
{
//echo date("Y", strtotime($i . '-01-01')).' '.date("L", strtotime($i . '-01-01')).'<br/>';
if (date("L", strtotime($i . '-01-01')) == 1)
$leap_years++;
}
return $leap_years;
}
echo leap_year($birth_date, $current_date);
答案 1 :(得分:-1)
也许你错过了闰年,这可以解释9天的差异,例如
9 * 4 y = 36 years