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

来源:互联网 发布:人工智能 军民融合 编辑:程序博客网 时间:2024/05/18 00:50
<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 '-';        }            }






0 0
原创粉丝点击