function age($birthday){
$birthday = strtotime($birthday);//int strtotime ( string $time [, int $now ] )
$year = date('Y', $birthday);
if(($month = (date('m') - date('m', $birthday))) < 0){
$year++;
}else if ($month == 0 && date('d') - date('d', $birthday) < 0){
$year++;
}
return date('Y') - $year;
}
此外还有一个相对简单的解决方法:需要把 生日 unix时间戳,然后得出当前的时间戳差,最后除以一年的时间戳
function age($birthday) {
if (strtotime($birthday) > 0){
return (int)((time() - strtotime($birthday))/(86400 * 365)) .'岁';
}else{
return '-';
}
}