本文實例講述了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;
}
PS:本站還提供了一個Unix時間戳轉換工具,包含了各種常見語言針對時間戳的操作方法,提供給大家參考:
Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數學運算技巧總結》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《php正則表達式用法總結》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。