计算年龄 精确到天

WBOY
Freigeben: 2016-07-25 08:46:37
Original
1629 Leute haben es durchsucht
用于计算年龄 精确到天
  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. * 计算年龄精准到年月日
  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 $bmonth = '0' . $bmonth;
  18. }
  19. if ($bday $bday = '0' . $bday;
  20. }
  21. $bi = intval($byear . $bmonth . $bday);
  22. $ni = intval($year . $month . $day);
  23. $not_birth = 0;
  24. if ($bi > $ni) {
  25. $not_birth = 1;
  26. $tmp = array($byear, $bmonth, $bday);
  27. list($byear, $bmonth, $bday) = array($year, $month, $day);
  28. list($year, $month, $day) = $tmp;
  29. list($bi, $ni) = array($ni, $bi);
  30. }
  31. $years = 0;
  32. while (($bi + 10000) $bi += 10000;
  33. $years++;
  34. $byear++;
  35. }//得到岁数后 抛弃年
  36. list($m, $d) = $this->getMD(array($year, $month, $day), array($byear, $bmonth, $bday));
  37. return array('year' => $years, 'month' => $m, 'day' => $d, 'not_birth' => $not_birth);
  38. }
  39. /**
  40. * 只能用于一年内计算
  41. * @param type $ymd
  42. * @param type $bymd
  43. */
  44. public function getMD($ymd, $bymd) {
  45. list($y, $m, $d) = $ymd;
  46. list($by, $bm, $bd) = $bymd;
  47. if (($m . $d) $m +=12;
  48. }
  49. $month = 0;
  50. while ((($bm . $bd) + 100) $bm++;
  51. $month++;
  52. }
  53. if ($bd $day = $d - $bd;
  54. } else {//少一个月
  55. $mdays = $bm > 12 ? $this->_getMothDay( ++$by, $bm - 12) : $this->_getMothDay($by, $bm);
  56. $day = $mdays - $bd + $d;
  57. }
  58. return array($month, $day);
  59. }
  60. private function _getMothDay($year, $month) {
  61. switch ($month) {
  62. case 1:
  63. case 3:
  64. case 5:
  65. case 7:
  66. case 8:
  67. case 10:
  68. case 12:
  69. $day = 31;
  70. break;
  71. case 2:
  72. $day = (intval($year % 4) ? 28 : 29); //能被4除尽的为29天其他28天
  73. break;
  74. default:
  75. $day = 30;
  76. break;
  77. }
  78. return $day;
  79. }
  80. }
  81. $cage = new Age();
  82. $test = array(
  83. '1990-06-12',
  84. '1990-07-13',
  85. '1990-08-16',
  86. '1990-10-10',
  87. '1990-10-13',
  88. '1990-10-15',
  89. '1990-11-9',
  90. '1990-11-22',
  91. '2016-11-22',
  92. '2016-8-22',
  93. '2016-10-13',
  94. );
  95. echo date('Y-m-d');
  96. echo '
    ';
    Nach dem Login kopieren
  97. foreach($test as $v){
  98. $tmp = $cage->calAge($v);
  99. echo $v , ':', $tmp['year'], '年', $tmp['month'],
  100. '月', $tmp['day'], '天', ';', $tmp['not_birth'], '
    ';
  101. }
  102. 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. */
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!