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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-02 我要评论

本文实例讲述了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.softyun.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.softyun.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.softyun.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.softyun.net/code/unixtime

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们