PHP 计算年龄
2022-08-30 06:29:41
我正在寻找一种方法来计算一个人的年龄,因为他们的DOB格式为dd / mm / yyyy。
我使用以下功能,该功能运行了几个月,直到某种故障导致while循环永无止境,并将整个站点磨成停止。由于每天有近100,000个DOB多次通过此功能,因此很难确定导致这种情况的原因。
有没有人有更可靠的方法来计算年龄?
//replace / with - so strtotime works
$dob = strtotime(str_replace("/","-",$birthdayDate));
$tdate = time();
$age = 0;
while( $tdate > $dob = strtotime('+1 year', $dob))
{
++$age;
}
return $age;
编辑:此函数似乎在某些时候工作正常,但对于 DOB 为 14/09/1986 返回“40”
return floor((time() - strtotime($birthdayDate))/31556926);