function birthday($birthday){
$age = strtotime($birthday);
if ($age === false) {
// 传入为空返回假
return false;
}
// explode : 把字符打散成数组
// 将生日年月日打到一个数组中
list($y1,$m1,$d1) = explode("-", date("Y-m-d",$age));
$now = strtotime("now");
list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
$age = $y2 - $y1;
// 如果现在的月份日期小于生日的 那么就是未满一周岁 age—1
if((int)($m2.$d2) < (int)($m1.$d1))
$age -= 1;
return $age;
}
echo birthday('2000-07-22');
方法二:date( )
简便一点:
function birthday($birthday){
list($year,$month,$day)=explode("-", $birthday);
// date() 函数 里面填 Y。。。。 直接转化当前时间
$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;
}
echo birthday("2000-01-25");
都没问题的啦 不展示截图了