Calculating the difference between dates in years, months, and days


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function smoothdate ($year, $month, $day)
  2. {
  3. return sprintf ('%04d', $year) . sprintf ('%02d', $month) . sprintf ('%02d', $day);
  4. }
  5.  
  6.  
  7. /*
  8.   function date_difference calculates the difference between two dates in
  9.   years, months, and days. There is a ColdFusion funtion called, I
  10.   believe, date_diff() which performs a similar function.
  11.  
  12.   It does not make use of 32-bit unix timestamps, so it will work for dates
  13.   outside the range 1970-01-01 through 2038-01-19. This function works by
  14.   taking the earlier date finding the maximum number of times it can
  15.   increment the years, months, and days (in that order) before reaching
  16.   the second date. The function does take yeap years into account, but does
  17.   not take into account the 10 days removed from the calendar (specifically
  18.   October 5 through October 14, 1582) by Pope Gregory to fix calendar drift.
  19.  
  20.   As input, it requires two associative arrays of the form:
  21.   array ( 'year' => year_value,
  22.   'month' => month_value.
  23.   'day' => day_value)
  24.  
  25.   The first input array is the earlier date, the second the later date. It
  26.   will check to see that the two dates are well-formed, and that the first
  27.   date is earlier than the second.
  28.  
  29.   If the function can successfully calculate the difference, it will return
  30.   an array of the form:
  31.   array ( 'years' => number_of_years_different,
  32.   'months' => number_of_months_different,
  33.   'days' => number_of_days_different)
  34.  
  35.   If the function cannot calculate the difference, it will return FALSE.
  36.  
  37. */
  38.  
  39. function date_difference ($first, $second)
  40. {
  41. $month_lengths = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  42.  
  43. $retval = FALSE;
  44.  
  45. if ( checkdate($first['month'], $first['day'], $first['year']) &&
  46. checkdate($second['month'], $second['day'], $second['year'])
  47. )
  48. {
  49. $start = smoothdate ($first['year'], $first['month'], $first['day']);
  50. $target = smoothdate ($second['year'], $second['month'], $second['day']);
  51.  
  52. if ($start <= $target)
  53. {
  54. $add_year = 0;
  55. while (smoothdate ($first['year']+ 1, $first['month'], $first['day']) <= $target)
  56. {
  57. $add_year++;
  58. $first['year']++;
  59. }
  60.  
  61. $add_month = 0;
  62. while (smoothdate ($first['year'], $first['month'] + 1, $first['day']) <= $target)
  63. {
  64. $add_month++;
  65. $first['month']++;
  66.  
  67. if ($first['month'] > 12)
  68. {
  69. $first['year']++;
  70. $first['month'] = 1;
  71. }
  72. }
  73.  
  74. $add_day = 0;
  75. while (smoothdate ($first['year'], $first['month'], $first['day'] + 1) <= $target)
  76. {
  77. if (($first['year'] % 100 == 0) && ($first['year'] % 400 == 0))
  78. {
  79. $month_lengths[1] = 29;
  80. }
  81. else
  82. {
  83. if ($first['year'] % 4 == 0)
  84. {
  85. $month_lengths[1] = 29;
  86. }
  87. }
  88.  
  89. $add_day++;
  90. $first['day']++;
  91. if ($first['day'] > $month_lengths[$first['month'] - 1])
  92. {
  93. $first['month']++;
  94. $first['day'] = 1;
  95.  
  96. if ($first['month'] > 12)
  97. {
  98. $first['month'] = 1;
  99. }
  100. }
  101.  
  102. }
  103.  
  104. $retval = array ('years' => $add_year, 'months' => $add_month, 'days' => $add_day);
  105. }
  106. }
  107.  
  108. return $retval;
  109. }
  110.  
  111.  
  112. /*
  113.   This code is merely an example of use of the function
  114. */
  115.  
  116. print '<pre>';
  117. $begin = array ('year' => 2001, 'month' => 3, 'day' => 14);
  118. $end = array ('year' => 2004, 'month' => 3, 'day' => 14);
  119.  
  120. $foo = date_difference ($begin, $end);
  121. if ($foo !== FALSE)
  122. {
  123. print_r ($foo);
  124. }
  125. else
  126. {
  127. print 'FALSE';
  128. }
  129.  
  130. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.