PHP常用函数之根据生日计算年龄功能示例

 4850

这篇文章主要介绍了PHP常用函数之根据生日计算年龄功能,结合实例形式分析了php日期相关转换与计算操作技巧,需要的朋友可以参考下


PHP常用函数之根据生日计算年龄功能示例


  1. /**
  2.  * 根据出生年月日计算出年龄
  3.  * @param $birth_year 
  4.  * @param $birth_month
  5.  * @param $birth_day
  6.  * @return int
  7.  */
  8. function getAgeByBirth($birth_year,$birth_month,$birth_day){
  9.     if(empty($birth_year) || empty($birth_month) || empty($birth_day)){
  10.         return 0;
  11.     }
  12.     $current_year = date('Y',time());
  13.     $current_month = date('m',time());
  14.     $current_day = date('d',time());
  15.     if($birth_year >= $current_year){
  16.         return 0;
  17.     }
  18.     $age = $current_year - $birth_year - 1;
  19.     if($current_month>$birth_month){
  20.         return $age+1;
  21.     }else if($current_month == $birth_month && $current_day>=$birth_day){
  22.         return $age+1;
  23.     }else{
  24.         return $age;
  25.     }
  26. }
  27. //测试:
  28. echo getAgeByBirth('1988','8','8');

运行结果:

31

TAG标签:
本文网址:https://www.zztuku.com/detail-7691.html
站长图库 - PHP常用函数之根据生日计算年龄功能示例
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐