PHP常用函數之根據生日計算年齡功能示例
本文實例講述了PHP常用函數之根據生日計算年齡功能。分享給大家供大家參考,具體如下:
/**
* 根據出生年月日計算出年齡
* @param $birth_year
* @param $birth_month
* @param $birth_day
* @return int
*/
function getAgeByBirth($birth_year,$birth_month,$birth_day){
if(empty($birth_year) || empty($birth_month) || empty($birth_day)){
return 0;
}
$current_year = date('Y',time());
$current_month = date('m',time());
$current_day = date('d',time());
if($birth_year >= $current_year){
return 0;
}
$age = $current_year - $birth_year - 1;
if($current_month>$birth_month){
return $age+1;
}else if($current_month == $birth_month && $current_day>=$birth_day){
return $age+1;
}else{
return $age;
}
}
//測試:
echo getAgeByBirth('1988','8','8');
運行結果:
31
PS:這里再為大家推薦幾款時間及日期相關工具供大家參考:
在線日期/天數計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
ajax+php打造進度條 readyState各狀態(tài)
php 打造進度條 readyState各狀態(tài),需要的朋友可以參考下。2010-03-03
靜態(tài)html文件執(zhí)行php語句的方法(推薦)
下面小編就為大家?guī)硪黄o態(tài)html文件執(zhí)行php語句的方法(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

