ホームページ  >  記事  >  バックエンド開発  >  PHP は 2 つの日付の差を計算します

PHP は 2 つの日付の差を計算します

WBOY
WBOYオリジナル
2016-07-25 08:42:471501ブラウズ
  1. $date1 = date( 'Y-m-d' );
  2. $date2 = "2015-12-04";
  3. $diff = abs(strtotime($date2) - strtotime($date1));
  4. $年 = 床($差 / (365*60*60*24));
  5. $月 = 床(($差 - $年 * 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 年、%d か月、%d 日n"、$ 年、$ か月、$ 日);
  8. -------------------------- ----------------------------- OR
  9. $date1 = new DateTime("2007-03-24");
  10. $date2 = new DateTime("2009-06-26");
  11. $interval = $date1->diff($date2);
  12. echo "差分 " 。 $interval->y 。 「年」。 $interval->m." 月、".$interval->d." 日 ";
  13. // は日数の合計を表示します(上記のように年、月、日に分けられていません)
  14. echo "差" 。 $interval->days 。 「 日 」;
  15. -------------------------------------------- ----------- または
  16. /**
  17. * 正確なセマンティクスを使用して 2 つの日付間の差異を計算します。 PHP に基づいて DateTime::diff()
  18. * Derick Rethans による実装。 2011 年 5 月 2 日に Emil H によって PHP に移植されました。権利は留保されません。
  19. *
  20. * 元のコードについては、こちらを参照してください:
  21. * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&view=マークアップ
  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. $うるう年 = $年 % 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. * 2 つの UNIX タイムスタンプを受け入れます。
  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


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。