函数是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函数使用,可以关注其他相关文章。

dawei

【声明】:绥化站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。