PHP calculates the difference between two dates

Source: Internet
Author: User
Tags date1
PHP calculates the difference between two dates

  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 pided 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 e il 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 );
  39. $ Days_in_month = array (31, 31, 28, 31, 30, 31, 30, 31, 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 = "19:37:22 ";
  110. Echo'
    ';
  111. print_r( _date_diff( strtotime($date), time() ) );
  112. echo '
  113. ';
  114. ?>


PHP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.