Maison  >  Article  >  développement back-end  >  PHP计算两个日期的差

PHP计算两个日期的差

WBOY
WBOYoriginal
2016-07-25 08:42:471501parcourir
  1. $date1 = date( 'Y-m-d' );
  2. $date2 = "2015-12-04";
  3. $diff = abs(strtotime($date2) - strtotime($date1));
  4. $years = floor($diff / (365*60*60*24));
  5. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  6. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  7. printf("%d years, %d months, %d days\n", $years, $months, $days);
  8. -------------------------------------------------------- OR
  9. $date1 = new DateTime("2007-03-24");
  10. $date2 = new DateTime("2009-06-26");
  11. $interval = $date1->diff($date2);
  12. echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
  13. // shows the total amount of days (not divided into years, months and days like above)
  14. echo "difference " . $interval->days . " days ";
  15. -------------------------------------------------------- OR
  16. /**
  17. * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
  18. * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No rights reserved.
  19. *
  20. * See here for original code:
  21. * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&view=markup
  22. * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&view=markup
  23. */
  24. function _date_range_limit($start, $end, $adj, $a, $b, $result)
  25. {
  26. if ($result[$a] $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1;
  27. $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1);
  28. }
  29. if ($result[$a] >= $end) {
  30. $result[$b] += intval($result[$a] / $adj);
  31. $result[$a] -= $adj * intval($result[$a] / $adj);
  32. }
  33. return $result;
  34. }
  35. function _date_range_limit_days($base, $result)
  36. {
  37. $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  38. $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  39. _date_range_limit(1, 13, 12, "m", "y", &$base);
  40. $year = $base["y"];
  41. $month = $base["m"];
  42. if (!$result["invert"]) {
  43. while ($result["d"] $month--;
  44. if ($month $month += 12;
  45. $year--;
  46. }
  47. $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  48. $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  49. $result["d"] += $days;
  50. $result["m"]--;
  51. }
  52. } else {
  53. while ($result["d"] $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  54. $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  55. $result["d"] += $days;
  56. $result["m"]--;
  57. $month++;
  58. if ($month > 12) {
  59. $month -= 12;
  60. $year++;
  61. }
  62. }
  63. }
  64. return $result;
  65. }
  66. function _date_normalize($base, $result)
  67. {
  68. $result = _date_range_limit(0, 60, 60, "s", "i", $result);
  69. $result = _date_range_limit(0, 60, 60, "i", "h", $result);
  70. $result = _date_range_limit(0, 24, 24, "h", "d", $result);
  71. $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  72. $result = _date_range_limit_days(&$base, &$result);
  73. $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  74. return $result;
  75. }
  76. /**
  77. * Accepts two unix timestamps.
  78. */
  79. function _date_diff($one, $two)
  80. {
  81. $invert = false;
  82. if ($one > $two) {
  83. list($one, $two) = array($two, $one);
  84. $invert = true;
  85. }
  86. $key = array("y", "m", "d", "h", "i", "s");
  87. $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one))));
  88. $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two))));
  89. $result = array();
  90. $result["y"] = $b["y"] - $a["y"];
  91. $result["m"] = $b["m"] - $a["m"];
  92. $result["d"] = $b["d"] - $a["d"];
  93. $result["h"] = $b["h"] - $a["h"];
  94. $result["i"] = $b["i"] - $a["i"];
  95. $result["s"] = $b["s"] - $a["s"];
  96. $result["invert"] = $invert ? 1 : 0;
  97. $result["days"] = intval(abs(($one - $two)/86400));
  98. if ($invert) {
  99. _date_normalize(&$a, &$result);
  100. } else {
  101. _date_normalize(&$b, &$result);
  102. }
  103. return $result;
  104. }
  105. $date = "2014-12-04 19:37:22";
  106. echo '
    ';
  107. print_r( _date_diff( strtotime($date), time() ) );
  108. echo '';
  109. ?>
复制代码

PHP


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn