我需要从提供的 2 个日期中获取年数。这是我的代码:
function daysDifference($endDate, $beginDate)
{
$date_parts1=explode("-", $beginDate);
$date_parts2=explode("-", $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
$diff = $end_date - $start_date;
echo $diff;
$years = floor($diff / (365.25*60*60*24));
return $years;
}
echo daysDifference('2011-03-12','2008-03-09');
$diff
给出一个数字输出。当我返回 $years
时,我得到了 0
。我做错了什么?
最佳答案
$d1 = new DateTime('2011-03-12');
$d2 = new DateTime('2008-03-09');
$diff = $d2->diff($d1);
echo $diff->y;
关于php - 计算PHP中两个日期之间的年数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5387166/