本文實例講述了php簡單計算年齡的方法。分享給大家供大家參考,具體如下:
/*** $date是時間戳* $type為1的時候是虛歲,2的時候是周歲*/function getAgeByBirth($date,$type = 1){ $nowYear = date("Y",time()); $nowMonth = date("m",time()); $nowDay = date("d",time()); $birthYear = date("Y",$date); $birthMonth = date("m",$date); $birthDay = date("d",$date); if($type == 1){ $age = $nowYear - ($birthYear - 1); }else{$type == 2}{ if($nowMonth<$birthMonth){ $age = $nowYear - $birthYear - 1; }elseif($nowMonth==$birthMonth){ if($nowDay<$birthDay){ $age = $nowYear - $birthYear - 1; }else{ $age = $nowYear - $birthYear; } }else{ $age = $nowYear - $birthYear; } } return $age;}希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選