分享PHP计算两个日期相差天数的代码

这篇文章主要为大家分享了PHP计算两个日期差的代码,实例分析了php操作日期的技巧,需要的朋友可以参考下,本文实例讲述了php计算两个日期相差天数的方法,分享给大家供大家参考,具体实现方法如下:

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

希望本文所述对大家学习php程序设计有所帮助。