Posted in PHP onOctober 21, 2019
本文实例讲述了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','8','8');
运行结果:
31
PHP常用函数之根据生日计算年龄功能示例
- Author -
学知无涯声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@