本文实例讲述了PHP根据生日计算年龄的方法。分享给大家供大家参考。具体如下:
PHP;">
PHP
function birthday($birthday){
$age = strtotime($birthday);
if($age === false){
return false;
}
list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
$now = strtotime("now");
list($y2,$m2,$d2) = explode("-",$now));
$age = $y2 - $y1;
if((int)($m2.$d2) < (int)($m1.$d1))
$age -= 1;
return $age;
}
echo birthday('1986-07-22');
?>
再来个简单点的,感觉没有上面的高大上
PHP;">
PHP
echo birthday("1989-01-25");
function birthday2($birthday){
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 || $month_diff < 0)
$year_diff--;
return $year_diff;
}
希望本文所述对大家的PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/21506.html