国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php計算年齡精準到年月日

2024-05-04 23:40:28
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php計算年齡精準到年月日的方法,涉及php操作日期與字符串的相關技巧,非常簡單實用,需要的朋友可以參考下

本文實例講述了php計算年齡精準到年月日的方法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2.  
  3. /* 
  4. * To change this license header, choose License Headers in Project Properties. 
  5. * To change this template file, choose Tools | Templates 
  6. * and open the template in the editor. 
  7. */ 
  8.  
  9. class Age { 
  10.  
  11. /** 
  12. * 計算年齡精準到年月日 
  13. * @param type $birthday 
  14. * @return array 
  15. */ 
  16.  
  17. public function calAge($birthday) { 
  18. list($byear, $bmonth, $bday) = explode('-', $birthday); 
  19. list($year, $month, $day) = explode('-', date('Y-m-d')); 
  20. $bmonth = intval($bmonth); 
  21. $bday = intval($bday); 
  22. if ($bmonth < 10) { 
  23. $bmonth = '0' . $bmonth; 
  24. if ($bday < 10) { 
  25. $bday = '0' . $bday; 
  26. $bi = intval($byear . $bmonth . $bday); 
  27. $ni = intval($year . $month . $day); 
  28. $not_birth = 0; 
  29. if ($bi > $ni) { 
  30. $not_birth = 1; 
  31. $tmp = array($byear, $bmonth, $bday); 
  32. list($byear, $bmonth, $bday) = array($year, $month, $day); 
  33. list($year, $month, $day) = $tmp; 
  34. list($bi, $ni) = array($ni, $bi); 
  35. $years = 0; 
  36. while (($bi + 10000) <= $ni) {//先取歲數 
  37. $bi += 10000; 
  38. $years++; 
  39. $byear++; 
  40. }//得到歲數后 拋棄年 
  41. list($m, $d) = $this->getMD(array($year, $month, $day), array($byear, $bmonth, $bday)); 
  42. return array('year' => $years, 'month' => $m, 'day' => $d, 'not_birth' => $not_birth); 
  43.  
  44. /** 
  45. * 只能用于一年內計算 
  46. * @param type $ymd 
  47. * @param type $bymd 
  48. */ 
  49. public function getMD($ymd, $bymd) { 
  50. list($y, $m, $d) = $ymd; 
  51. list($by, $bm, $bd) = $bymd; 
  52. if (($m . $d) < ($bm . $bd)) { 
  53. $m +=12; 
  54. $month = 0; 
  55. while ((($bm . $bd) + 100) <= ($m . $d)) { 
  56. $bm++; 
  57. $month++; 
  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. return array($month, $day); 
  64.  
  65. private function _getMothDay($year, $month) { 
  66. switch ($month) { 
  67. case 1: 
  68. case 3: 
  69. case 5: 
  70. case 7: 
  71. case 8: 
  72. case 10: 
  73. case 12: 
  74. $day = 31; 
  75. break
  76. case 2: 
  77. $day = (intval($year % 4) ? 28 : 29); //能被4除盡的為29天其他28天 
  78. break
  79. default
  80. $day = 30; 
  81. break
  82. return $day; 
  83.  
  84.  
  85. $cage = new Age(); 
  86. $test = array( 
  87. '1990-06-12'
  88. '1990-07-13'
  89. '1990-08-16'
  90. '1990-10-10'
  91. '1990-10-13'
  92. '1990-10-15'
  93. '1990-11-9'
  94. '1990-11-22'
  95. '2016-11-22'
  96. '2016-8-22'
  97. '2016-10-13'
  98. ); 
  99. echo date('Y-m-d'); 
  100. echo '<pre>'
  101. foreach($test as $v){ 
  102. $tmp = $cage->calAge($v); 
  103. echo $v , ':', $tmp['year'], '年', $tmp['month'], 
  104. '月', $tmp['day'], '天'';', $tmp['not_birth'], '<br>'
  105. echo '</pre>' ; 
  106.  
  107.  
  108. /* 
  109. 運行結果: 
  110. 2015-10-13 
  111. 1990-06-12:25年4月1天;0 
  112. 1990-07-13:25年3月0天;0 
  113. 1990-08-16:25年1月27天;0 
  114. 1990-10-10:25年0月3天;0 
  115. 1990-10-13:25年0月0天;0 
  116. 1990-10-15:24年11月28天;0 
  117. 1990-11-9:24年11月4天;0 
  118. 1990-11-22:24年10月21天;0 
  119. 2016-11-22:1年1月9天;1 
  120. 2016-8-22:0年10月9天;1 
  121. 2016-10-13:1年0月0天;1 
  122.  
  123.  
  124. */ 

希望本文所述對大家學習php程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辉县市| 南宁市| 阳谷县| 樟树市| 平利县| 车致| 福海县| 兴宁市| 徐州市| 浑源县| 嘉鱼县| 秀山| 景德镇市| 收藏| 逊克县| 麻江县| 阿克苏市| 隆德县| 西藏| 香港 | 民乐县| 读书| 莱州市| 中方县| 阳山县| 岐山县| 丰镇市| 垫江县| 从化市| 巴青县| 克山县| 游戏| 峡江县| 南城县| 敖汉旗| 丽江市| 宾阳县| 濮阳市| 延长县| 都兰县| 安仁县|