废话不多说,之间上代码:
<?php/** * 根据出生年月日计算出年龄 * @param $birth_year * @param $birth_month * @param $birth_day * @return int */function getagebybirth($birth_year,$birth_month,$birth_day){ if(empty($birth_year) || empty($birth_month) || empty($birth_day)){ return 0; } $current_year = date('y',time()); $current_month = date('m',time()); $current_day = date('d',time()); if($birth_year >= $current_year){ return 0; } $age = $current_year - $birth_year - 1; if($current_month>$birth_month){ return $age+1; }else if($current_month == $birth_month && $current_day>=$birth_day){ return $age+1; }else{ return $age; }}//测试:echo getagebybirth('1988','4','8');?>
运行结果:
32
更多相关知识,请关注 !!
以上就是php实现根据出生年月日计算年龄的功能(代码示例)的详细内容。
