首頁  >  文章  >  後端開發  >  php簡單計算年齡的方法

php簡單計算年齡的方法

墨辰丷
墨辰丷原創
2018-05-29 17:25:212875瀏覽

這篇文章主要介紹了php簡單計算年齡的方法,透過自訂函數實現針對週歲與虛歲的簡單計算功能,涉及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 使用__call重載

php編程之kindeditor上傳圖片加浮水印實作 

#PHP實作動態壓縮js與css檔案的方法

#

以上是php簡單計算年齡的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn