PHP根据出生日期计算年龄 发布时间:2023/10/11 php 日期 php通过出生日期计算出年龄,实现办法,首先将出生日期的年月日转化为时间戳,然后通过当前时间戳减去出生年月日的时间戳,从而计算出年龄。 详细代码如下: /** * 准备工作完毕 开始计算年龄函数 * @param $birthday 出生时间 uninx时间戳 * @param $time 当前时间 **/ function getAge($birthday){ //格式化出生时间年月日 $byear=date('Y',$birthday); $bmonth=date('m',$birthday); $bday=date('d',$birthday); //格式化当前时间年月日 $tyear=date('Y'); $tmonth=date('m'); $tday=date('d'); //开始计算年龄 $age=$tyear-$byear; if($bmonth>$tmonth || $bmonth==$tmonth && $bday>$tday){ $age--; } return $age; } $riqi='1989-10-18 15:20:36'; $uriqi=strtotime($riqi); //将日期转化为时间戳 $age=getAge($uriqi); echo '<br><br>年龄计算结果:'.$age.'岁';复制代码