這篇文章主要介紹了PHP常用函數(shù)之根據(jù)生日計(jì)算年齡功能,結(jié)合實(shí)例形式分析了php日期相關(guān)轉(zhuǎn)換與計(jì)算操作技巧,需要的朋友可以參考下

/**
* 根據(jù)出生年月日計(jì)算出年齡
* @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');運(yùn)行結(jié)果:
31






