Home > Backend Development > PHP Tutorial > Calculate age to the nearest day

Calculate age to the nearest day

WBOY
Release: 2016-07-25 08:46:37
Original
1714 people have browsed it
用于计算年龄 精确到天
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. class Age {
  7. /**
  8. * Calculate age accurately to year, month and day
  9. * @param type $birthday
  10. * @return array
  11. */
  12. public function calAge($birthday) {
  13. list($byear, $bmonth, $bday) = explode('-', $birthday);
  14. list($year, $month, $day) = explode('-', date('Y-m-d'));
  15. $bmonth = intval($bmonth);
  16. $bday = intval($bday);
  17. if ($bmonth < 10) {
  18. $bmonth = '0' . $bmonth;
  19. }
  20. if ($bday < 10) {
  21. $bday = '0' . $bday;
  22. }
  23. $bi = intval($byear . $bmonth . $bday);
  24. $ni = intval($year . $month . $day);
  25. $not_birth = 0;
  26. if ($bi > $ni) {
  27. $not_birth = 1;
  28. $tmp = array($byear, $bmonth, $bday);
  29. list($byear, $bmonth, $bday) = array($year, $month, $day);
  30. list($year, $month, $day) = $tmp;
  31. list($bi, $ni) = array($ni, $bi);
  32. }
  33. $years = 0;
  34. while (($bi + 10000) <= $ni) {//先取岁数
  35. $bi += 10000;
  36. $years++;
  37. $byear++;
  38. }//得到岁数后 抛弃年
  39. list($m, $d) = $this->getMD(array($year, $month, $day), array($byear, $bmonth, $bday));
  40. return array('year' => $years, 'month' => $m, 'day' => $d, 'not_birth' => $not_birth);
  41. }
  42. /**
  43. * Can only be used for calculations within one year
  44. * @param type $ymd
  45. * @param type $bymd
  46. */
  47. public function getMD($ymd, $bymd) {
  48. list($y, $m, $d) = $ymd;
  49. list($by, $bm, $bd) = $bymd;
  50. if (($m . $d) < ($bm . $bd)) {
  51. $m +=12;
  52. }
  53. $month = 0;
  54. while ((($bm . $bd) + 100) <= ($m . $d)) {
  55. $bm++;
  56. $month++;
  57. }
  58. if ($bd <= $d) {//同处一个月
  59. $day = $d - $bd;
  60. } else {//少一个月
  61. $mdays = $bm > 12 ? $this->_getMothDay( ++$by, $bm - 12) : $this->_getMothDay($by, $bm);
  62. $day = $mdays - $bd + $d;
  63. }
  64. return array($month, $day);
  65. }
  66. private function _getMothDay($year, $month) {
  67. switch ($month) {
  68. case 1:
  69. case 3:
  70. case 5:
  71. case 7:
  72. case 8:
  73. case 10:
  74. case 12:
  75. $day = 31;
  76. break;
  77. case 2:
  78. $day = (intval($year % 4) ? 28 : 29); //能被4除尽的为29天其他28天
  79. break;
  80. default:
  81. $day = 30;
  82. break;
  83. }
  84. return $day;
  85. }
  86. }
  87. $cage = new Age();
  88. $test = array(
  89. '1990-06-12',
  90. '1990-07-13',
  91. '1990-08-16',
  92. '1990-10-10',
  93. '1990-10-13',
  94. '1990-10-15',
  95. '1990-11-9',
  96. '1990-11-22',
  97. '2016-11-22',
  98. '2016-8-22',
  99. '2016-10-13',
  100. );
  101. echo date('Y-m-d');
  102. echo '
    ';</li>
    <li>foreach($test as $v){</li>
    <li>    $tmp = $cage->calAge($v);</li>
    <li>    echo $v , ':', $tmp['year'], '年', $tmp['month'],</li>
    <li> '月', $tmp['day'], '天', ';', $tmp['not_birth'], '<br>';</li>
    <li>}</li>
    <li>echo '
    ' ;
  103. /*
  104. 运行结果:
  105. 2015-10-13
  106. 1990-06-12:25年4月1天;0
  107. 1990-07-13:25年3月0天;0
  108. 1990-08-16:25年1月27天;0
  109. 1990-10-10:25年0月3天;0
  110. 1990-10-13:25年0月0天;0
  111. 1990-10-15:24年11月28天;0
  112. 1990-11-9:24年11月4天;0
  113. 1990-11-22:24年10月21天;0
  114. 2016-11-22:1年1月9天;1
  115. 2016-8-22:0年10月9天;1
  116. 2016-10-13:1年0月0天;1
  117. *
  118. */
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template