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

(0)

相关推荐

  • php计算两个日期时间差(返回年、月、日)

    在PHP程序中,很多时候都会遇到处理时间的问题,比如:判断用户在线了多长时间,共登录了多少天,两个帖子发布的时间差或者是不同操作之间的日志记录等等.在文章中,简单地举例介绍了PHP中如何计算两个日期相差 年.月.日. <?php /** +---------------------------------------------------------- * 功能:计算两个日期相差 年 月 日 +--------------------------------------------------

  • php根据生日计算年龄的方法

    本文实例讲述了php根据生日计算年龄的方法.分享给大家供大家参考.具体如下: <?php function birthday($birthday){ $age = strtotime($birthday); if($age === false){ return false; } list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); $now = strtotime("now"); list

  • PHP中UNIX时间戳和日期间的转换与计算实例

    UNIX时间戳是保存日期和时间的一种紧凑简洁的方法,是大多数UNIX系统中保存当前日期和时间的一种方法,也是在大多数计算机语言中表示日期和时间的一种标准格式.以32位整数表示格林威治标准时间,例如,使用证书11230499325表示当前时间的时间戳.UNIX时间戳是从1970年1月1日零点(UTC/GMT的午夜)开始起到当前时间所经过的秒数.1970年1月1日零点作为所有日期计算的基础,这个日期通常成为UNIX纪元. 因为UNIX时间戳是一个32位的数字格式,所以特别适用于计算机处理,例如计算两

  • php根据身份证号码计算年龄的实例代码

    复制代码 代码如下: <?php function getAgeByID($id){         //过了这年的生日才算多了1周岁         if(empty($id)) return '';         $date=strtotime(substr($id,6,8)); //获得出生年月日的时间戳         $today=strtotime('today'); //获得今日的时间戳         $diff=floor(($today-$date)/86400/365);

  • PHP 年龄计算函数(精确到天)

    复制代码 代码如下: <?php /** * PHP 年龄计算函数 * * 参数支持数组传参和标准的 Mysql date 类型传参 * params sample * -------------------------------------------------- $birthArr = array( 'year' => '2000', 'month' => '11', 'day' => '3' ); $birthStr = '2000-11-03'; * ---------

  • php计算到指定日期还有多少天的方法

    本文实例讲述了php计算到指定日期还有多少天的方法.分享给大家供大家参考.具体如下: function countdays($d) { $olddate = substr($d, 4); $newdate = date(Y) ."".$olddate; $nextyear = date(Y)+1 ."".$olddate; if($newdate > date("Y-m-d")) { $start_ts = strtotime($newda

  • php简单计算年龄的方法(周岁与虚岁)

    本文实例讲述了php简单计算年龄的方法.分享给大家供大家参考,具体如下: /** * $date是时间戳 * $type为1的时候是虚岁,2的时候是周岁 */ function getAgeByBirth($date,$type = 1){ $nowYear = date("Y",time()); $nowMonth = date("m",time()); $nowDay = date("d",time()); $birthYear = date

  • php计算两个日期相差天数的方法

    本文实例讲述了php计算两个日期相差天数的方法.分享给大家供大家参考.具体实现方法如下: <?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day2 * @return number */ function diffBetweenTwoDays ($day1, $day2) { $second1 = strtotime($day1); $second2 = s

  • PHP计算指定日期所在周的开始和结束日期的方法

    本文实例讲述了PHP计算指定日期所在周的开始和结束日期的方法.分享给大家供大家参考.具体实现方法如下: <html> <head> <title>计算一周开始结束日期</title> </head> <body> <form method="post" action="./index.html" enctype="utf-8"> <table> <

  • php计算年龄精准到年月日

    本文实例讲述了php计算年龄精准到年月日的方法.分享给大家供大家参考.具体如下: <?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ class Age { /** * 计算年龄

随机推荐