php根据出生日期计算年龄
方法一:
- <?php
- function birthday($mydate){
- $birth=$mydate;
- list($by,$bm,$bd)=explode('-',$birth);
- $cm=date('n');
- $cd=date('j');
- $age=date('Y')-$by-1;
- if ($cm>$bm || $cm==$bm && $cd>$bd) $age++;
- return $age;
- }
- echo birthday('2010-01-01');
- ?>
方法二:
- <?php
- $birthday = '2010-01-01';
- $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
- if(date('m', time()) == date('m', strtotime($birthday))){
- if(date('d', time()) > date('d', strtotime($birthday))){
- $age++;
- }
- }
- elseif(date('m', time()) > date('m', strtotime($birthday))){
- $age++;
- }
- echo $age;
- ?>
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1168369,如需转载请自行联系原作者