PHP中经常涉及到数据库中有用户出生年月日,然后需要根据当前时间获得用户年龄,项目中的需求。
/**
* 根据出生年月日获取用户年龄
* @param [date] $birthday [年月日格式Y-m-d]
* @return [int] $age [年龄]
*/
function get_age($birthday){
if($birthday == "0000-00-00"){
return "";
exit();
}else{
$birthday = strtotime($birthday);
$year = date('Y', $birthday);
if(($month = (date('m') - date('m', $birthday))) < 0){
$year++;
}else if ($month == 0 && date('d') - date('d', $birthday) < 0){
$year++;
}
$age = date('Y') - $year;
return $age;
}
}
PHP根据出生年月日获取用户年龄函数
原文链接:https://beltxman.com/1549.html,若无特殊说明本站内容为 行星带 原创,未经同意禁止转载。