Home  >  Article  >  Backend Development  >  PHP calculates the difference between two dates

PHP calculates the difference between two dates

WBOY
WBOYOriginal
2016-07-25 08:42:471560browse
  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 daysn", $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] < $start) {
  27. $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1;
  28. $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1);
  29. }
  30. if ($result[$a] >= $end) {
  31. $result[$b] += intval($result[$a] / $adj);
  32. $result[$a] -= $adj * intval($result[$a] / $adj);
  33. }
  34. return $result;
  35. }
  36. function _date_range_limit_days($base, $result)
  37. {
  38. $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  39. $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  40. _date_range_limit(1, 13, 12, "m", "y", &$base);
  41. $year = $base["y"];
  42. $month = $base["m"];
  43. if (!$result["invert"]) {
  44. while ($result["d"] < 0) {
  45. $month--;
  46. if ($month < 1) {
  47. $month += 12;
  48. $year--;
  49. }
  50. $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  51. $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  52. $result["d"] += $days;
  53. $result["m"]--;
  54. }
  55. } else {
  56. while ($result["d"] < 0) {
  57. $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  58. $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  59. $result["d"] += $days;
  60. $result["m"]--;
  61. $month++;
  62. if ($month > 12) {
  63. $month -= 12;
  64. $year++;
  65. }
  66. }
  67. }
  68. return $result;
  69. }
  70. function _date_normalize($base, $result)
  71. {
  72. $result = _date_range_limit(0, 60, 60, "s", "i", $result);
  73. $result = _date_range_limit(0, 60, 60, "i", "h", $result);
  74. $result = _date_range_limit(0, 24, 24, "h", "d", $result);
  75. $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  76. $result = _date_range_limit_days(&$base, &$result);
  77. $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  78. return $result;
  79. }
  80. /**
  81. * Accepts two unix timestamps.
  82. */
  83. function _date_diff($one, $two)
  84. {
  85. $invert = false;
  86. if ($one > $two) {
  87. list($one, $two) = array($two, $one);
  88. $invert = true;
  89. }
  90. $key = array("y", "m", "d", "h", "i", "s");
  91. $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one))));
  92. $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two))));
  93. $result = array();
  94. $result["y"] = $b["y"] - $a["y"];
  95. $result["m"] = $b["m"] - $a["m"];
  96. $result["d"] = $b["d"] - $a["d"];
  97. $result["h"] = $b["h"] - $a["h"];
  98. $result["i"] = $b["i"] - $a["i"];
  99. $result["s"] = $b["s"] - $a["s"];
  100. $result["invert"] = $invert ? 1 : 0;
  101. $result["days"] = intval(abs(($one - $two)/86400));
  102. if ($invert) {
  103. _date_normalize(&$a, &$result);
  104. } else {
  105. _date_normalize(&$b, &$result);
  106. }
  107. return $result;
  108. }
  109. $date = "2014-12-04 19:37:22";
  110. echo '
    ';</li>
    <li>print_r( _date_diff( strtotime($date), time() ) );</li>
    <li>echo '
    ';
  111. ?>
复制代码

PHP


Statement:
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