设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php如何实现利用生日算年龄功能?

发布时间:2021-12-30 13:10 所属栏目:121 来源:互联网
导读:函数是PHP学习需要掌握的内容,为了让大家更熟悉PHP函数的应用,下面给大家分享基于PHP实现的根据生日计算年龄的代码,对新手有一定的借鉴学习价值,感兴趣的朋友可以参考参考。 /** * 根据出生年月日计算出年龄 * @param $birth_year * @param $birth_month
    函数是PHP学习需要掌握的内容,为了让大家更熟悉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
 
       以上就是利用php根据生日计算年龄的方法介绍,上述代码仅供大家参考,需要的朋友可以看看,希望本文对大家有帮助。想要了解更多PHP函数使用,可以关注其他相关文章。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读