您的位置:首页 > 编程语言 > PHP开发

php根据出生日期获取年龄,生日数据类型为date型

2014-12-23 17:10 681 查看
<span style="font-size:18px;"><strong>

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;
}</strong></span>


此外还有一个相对简单的解决方法:需要把 生日 unix时间戳,然后得出当前的时间戳差,最后除以一年的时间戳

function age($birthday) {
if (strtotime($birthday) > 0){
return (int)((time() - strtotime($birthday))/(86400 * 365)) .'岁';
}else{
return '-';
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: