php计算年龄精准到年月日的程序

这篇文章主要介绍了php计算年龄精准到年月日的方法,涉及php操作日期与字符串的相关技巧,非常简单实用,需要的朋友可以参考下

本文实例讲述了php计算年龄精准到年月日的方法。分享给大家供大家参考。具体如下:

 $ni) {
      $not_birth = 1;
      $tmp = array($byear, $bmonth, $bday);
      list($byear, $bmonth, $bday) = array($year, $month, $day);
      list($year, $month, $day) = $tmp;
      list($bi, $ni) = array($ni, $bi);
    }
    $years = 0;
    while (($bi + 10000) <= $ni) {//先取岁数
      $bi += 10000;
      $years++;
      $byear++;
    }//得到岁数后 抛弃年
    list($m, $d) = $this->getMD(array($year, $month, $day), array($byear, $bmonth, $bday));
    return array('year' => $years, 'month' => $m, 'day' => $d, 'not_birth' => $not_birth);
  }
 
  /**
   * 只能用于一年内计算
   * @param type $ymd
   * @param type $bymd
   */
  public function getMD($ymd, $bymd) {
    list($y, $m, $d) = $ymd;
    list($by, $bm, $bd) = $bymd;
    if (($m . $d) < ($bm . $bd)) {
      $m +=12;
    }
    $month = 0;
    while ((($bm . $bd) + 100) <= ($m . $d)) {
      $bm++;
      $month++;
    }
    if ($bd <= $d) {//同处一个月
      $day = $d - $bd;
    } else {//少一个月
      $mdays = $bm > 12 ? $this->_getMothDay( ++$by, $bm - 12) : $this->_getMothDay($by, $bm);
      $day = $mdays - $bd + $d;
    }
    return array($month, $day);
  }
 
  private function _getMothDay($year, $month) {
    switch ($month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        $day = 31;
        break;
      case 2:
        $day = (intval($year % 4) ? 28 : 29); //能被4除尽的为29天其他28天
        break;
      default:
        $day = 30;
        break;
    }
    return $day;
  }
 
}
 
$cage = new Age();
$test = array(
  '1990-06-12',
  '1990-07-13',
  '1990-08-16',
  '1990-10-10',
  '1990-10-13',
  '1990-10-15',
  '1990-11-9',
  '1990-11-22',
  '2016-11-22',
  '2016-8-22',
  '2016-10-13',
);
echo date('Y-m-d');
echo '
';
foreach($test as $v){
  $tmp = $cage->calAge($v);
  echo $v , ':', $tmp['year'], '年', $tmp['month'],
 '月', $tmp['day'], '天', ';', $tmp['not_birth'], '
';
}
echo '
' ;
 
 
/*
  运行结果:
  2015-10-13
  1990-06-12:25年4月1天;0
  1990-07-13:25年3月0天;0
  1990-08-16:25年1月27天;0
  1990-10-10:25年0月3天;0
  1990-10-13:25年0月0天;0
  1990-10-15:24年11月28天;0
  1990-11-9:24年11月4天;0
  1990-11-22:24年10月21天;0
  2016-11-22:1年1月9天;1
  2016-8-22:0年10月9天;1
  2016-10-13:1年0月0天;1
 
 *
 */
希望本文所述对大家学习php程序设计有所帮助

时间: 2024-09-20 04:23:44

php计算年龄精准到年月日的程序的相关文章

php计算年龄精准到年月日_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 { /** * 计算年龄

php根据出生日期计算年龄 生肖 星座程序

例1  代码如下 复制代码 function getAge($birthday) {  $age = 0;  $year = $month = $day = 0;  if (is_array($birthday)) {   extract($birthday);  } else {   if (strpos($birthday, '-') !== false) {    list($year, $month, $day) = explode('-', $birthday);    $day =

JS实现根据出生年月计算年龄

 本篇文章主要是对利用JS实现根据出生年月计算年龄的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 代码如下: <script language=javascript>      function   ages(str)      {            var   r   =   str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);              if(r==null)return   false;           

阅读器实现计算用户看书时间的程序逻辑

问题描述 阅读器实现计算用户看书时间的程序逻辑 各位大神好,我想问个问题,如果你用阅读器看书,计算你看了多长时间的程序逻辑是怎么写的啊?或者有没有别人已经写好的程序? 解决方案 网页还是App?这个你要说清楚哈.

用vbs实现配置无人登录计算机时使用的屏幕保护程序_vbs

问: 您好,脚本专家!我最近下载了你们的"脚本中心"屏幕保护程序,当有人登录计算机时,它运行起来棒极了.但是无人登录时,计算机却使用其它屏幕保护程序.如何让计算机在无人登录时也使用"脚本中心"屏幕保护程序? -- RF 答: 您好,RF.您知道,我们遇到过这种情况,您可能在设法欺骗这些老脚本专家们.举个例子来说,我们怎么知道您希望无人登录计算机时运行的屏幕保护程序就是我们的屏幕保护程序?也许您只是奉承脚本专家,好让脚本专家回答您的问题.回答完问题后,您就会把我们甩掉

asp.net 未在本地计算机上注册“OraOLEDB.Oracle”提供程序

问题描述 vs2013调试正常,通过iis访问ORACLE数据库,提示未在本地计算机上注册"OraOLEDB.Oracle"提供程序 解决方案 解决方案二:服务器上也要安装访问数据库的组件解决方案三:看看这篇文章:'OraOLEDB.Oracle'providerisnotregistered解决方案四:这里的讨论:OraOLEDB.Oracleproviderisnotregisteredonthelocalmachine

oracle 如何分岁/月等为单位计算年龄?

问题描述 oracle 如何分岁/月等为单位计算年龄? 有oracle一张表表中有一出生日期字段为date 类型是,现要建立一个视图 oracle 表如下 id---(varchar2(10)) date_of_birth---(date) 05576767 1957-3-28 05563743 ? ? ? ? ?2013-3-27 05563744 ? ? ? ? ?2013-7-15 05563745 ? ? ? ? ?2013-6-7 05563741? ? ? ? ? ?2010-11-

SQL通过日期计算年龄

原文:SQL通过日期计算年龄 首先建立一个表如下: ======================= BirthDay datetime not null Age 通过公式计算得出 ======================= 以上是表的两个字段,通过BirthDay字段的数据自动生成Age字段 Age字段的公式如下: (case when (datediff(year,[BirthDay],getdate()) <> 0) then (ltrim(datediff(year,[BirthDa

php简单计算年龄的方法(周岁与虚岁)_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