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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

用PHP控制用户的浏览器--ob*函数的使用说明

用PHP控制用户的浏览器--ob*函数的使用˂!-- google 的广告条 2005年09月20日换位置 唉,22号被停了.郁闷,没作弊呀 11.27日重开了 ˂!-...

PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程

PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下: 一、PHP检测页面 check.php...

php实现执行某一操作时弹出确认、取消对话框

复制代码 代码如下: <script> function del(){ if(confirm("确定要删除吗?")){ alert('删除成功!'); return true...

php下删除一篇文章生成的多个静态页面

复制代码 代码如下: //– 删除一篇文章生成的多个静态页面 //– 生成的文章名为 5.html 5_2.html 5_3.html /*—————————————————— */ f...

PHP 导出Excel示例分享

下载PHPExcel_1.8.0_doc.zip http://phpexcel.codeplex.com/,将解压后的文件夹里的Classes上传到网站的根目录下,Classes目录内...