Advertisement
AmourSpirit

How to calculate the difference between two dates using PHP

Jul 20th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.95 KB | None | 0 0
  1. <?php
  2. // Using environment library fiddle_envlib.php to files saved under user id folder
  3. //include "fiddle_envlib.php";
  4. // Source: https://abstractcodify.snipt.net/how-to-calculate-the-difference-between-two-dates-using-php-7c4b4df5/
  5.  
  6. // original source code has been modified to change the byref as show in the comments below
  7. // Wrong way!
  8. // myFunc(&$arg);               # Deprecated pass-by-reference argument
  9. // function myFunc($arg) { }
  10.  
  11. // Right way!
  12. // myFunc($var);                # pass-by-value argument
  13. // function myFunc(&$arg) { }
  14.  
  15. $date1 = date( 'Y-m-d' );
  16. $date2 = "2015-12-04";
  17.  
  18. $diff = abs(strtotime($date2) - strtotime($date1));
  19.  
  20. $years = floor($diff / (365*60*60*24));
  21. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  22. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  23.  
  24. printf("%d years, %d months, %d days\n", $years, $months, $days);
  25.  
  26. // -------------------------------------------------------- OR
  27.  
  28.  
  29. $date1 = new DateTime("2007-03-24");
  30. $date2 = new DateTime("2009-06-26");
  31. $interval = $date1->diff($date2);
  32. echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
  33.  
  34. // shows the total amount of days (not divided into years, months and days like above)
  35. echo "difference " . $interval->days . " days ";
  36.  
  37.  
  38. // -------------------------------------------------------- OR
  39.    
  40.    
  41. /**
  42.  * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
  43.  * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No rights reserved.
  44.  *
  45.  * See here for original code:
  46.  * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&view=markup
  47.  * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&view=markup
  48.  */
  49.  
  50. function _date_range_limit($start, $end, $adj, $a, $b, &$result)
  51. {
  52.     if ($result[$a] < $start) {
  53.         $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1;
  54.         $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1);
  55.     }
  56.  
  57.     if ($result[$a] >= $end) {
  58.         $result[$b] += intval($result[$a] / $adj);
  59.         $result[$a] -= $adj * intval($result[$a] / $adj);
  60.     }
  61.  
  62.     return $result;
  63. }
  64.  
  65. function _date_range_limit_days(&$base, &$result)
  66. {
  67.     $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  68.     $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  69.  
  70.     _date_range_limit(1, 13, 12, "m", "y", $base);
  71.  
  72.     $year = $base["y"];
  73.     $month = $base["m"];
  74.  
  75.     if (!$result["invert"]) {
  76.         while ($result["d"] < 0) {
  77.             $month--;
  78.             if ($month < 1) {
  79.                 $month += 12;
  80.                 $year--;
  81.             }
  82.  
  83.             $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  84.             $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  85.  
  86.             $result["d"] += $days;
  87.             $result["m"]--;
  88.         }
  89.     } else {
  90.         while ($result["d"] < 0) {
  91.             $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
  92.             $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
  93.  
  94.             $result["d"] += $days;
  95.             $result["m"]--;
  96.  
  97.             $month++;
  98.             if ($month > 12) {
  99.                 $month -= 12;
  100.                 $year++;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return $result;
  106. }
  107.  
  108. function _date_normalize(&$base, &$result)
  109. {
  110.     $result = _date_range_limit(0, 60, 60, "s", "i", $result);
  111.     $result = _date_range_limit(0, 60, 60, "i", "h", $result);
  112.     $result = _date_range_limit(0, 24, 24, "h", "d", $result);
  113.     $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  114.  
  115.     $result = _date_range_limit_days($base, $result);
  116.  
  117.     $result = _date_range_limit(0, 12, 12, "m", "y", $result);
  118.  
  119.     return $result;
  120. }
  121.  
  122. /**
  123.  * Accepts two unix timestamps.
  124.  */
  125. function _date_diff($one, $two)
  126. {
  127.     $invert = false;
  128.     if ($one > $two) {
  129.         list($one, $two) = array($two, $one);
  130.         $invert = true;
  131.     }
  132.  
  133.     $key = array("y", "m", "d", "h", "i", "s");
  134.     $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one))));
  135.     $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two))));
  136.  
  137.     $result = array();
  138.     $result["y"] = $b["y"] - $a["y"];
  139.     $result["m"] = $b["m"] - $a["m"];
  140.     $result["d"] = $b["d"] - $a["d"];
  141.     $result["h"] = $b["h"] - $a["h"];
  142.     $result["i"] = $b["i"] - $a["i"];
  143.     $result["s"] = $b["s"] - $a["s"];
  144.     $result["invert"] = $invert ? 1 : 0;
  145.     $result["days"] = intval(abs(($one - $two)/86400));
  146.  
  147.     if ($invert) {
  148.         _date_normalize($a, $result);
  149.     } else {
  150.         _date_normalize($b, $result);
  151.     }
  152.  
  153.     return $result;
  154. }
  155.  
  156. $date = "2014-12-04 19:37:22";
  157.  
  158. echo '<pre>';
  159. print_r( _date_diff( strtotime($date), time() ) );
  160. echo '</pre>';
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement