You are here

function date_difference in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_api.module \date_difference()
  2. 6 date_api.module \date_difference()

Compute difference between two days using a given measure.

Parameters

mixed $date1: the starting date

mixed $date2: the ending date

string $measure: 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'

string $type: the type of dates provided: DATE_OBJECT, DATE_DATETIME, DATE_ISO, DATE_UNIX, DATE_ARRAY

2 calls to date_difference()
date_generate in date/date_content_generate.inc
date_repeat_build_dates in date/date_repeat.inc
Helper function to build repeating dates from a $node_field.

File

./date_api.module, line 822
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

function date_difference($date1_in, $date2_in, $measure = 'seconds', $type = DATE_OBJECT) {

  // Create cloned objects or original dates will be impacted by
  // the date_modify() operations done in this code.
  $date1 = drupal_clone(date_convert($date1_in, $type, DATE_OBJECT));
  $date2 = drupal_clone(date_convert($date2_in, $type, DATE_OBJECT));
  if (is_object($date1) && is_object($date2)) {
    $diff = date_format($date2, 'U') - date_format($date1, 'U');
    if ($diff == 0) {
      return 0;
    }
    elseif ($diff < 0) {

      // Make sure $date1 is the smaller date.
      $temp = $date2;
      $date2 = $date1;
      $date1 = $temp;
      $diff = date_format($date2, 'U') - date_format($date1, 'U');
    }
    $year_diff = intval(date_format($date2, 'Y') - date_format($date1, 'Y'));
    switch ($measure) {

      // The easy cases first.
      case 'seconds':
        return $diff;
      case 'minutes':
        return $diff / 60;
      case 'hours':
        return $diff / 3600;
      case 'years':
        return $year_diff;
      case 'months':
        $format = 'n';
        $item1 = date_format($date1, $format);
        $item2 = date_format($date2, $format);
        if ($year_diff == 0) {
          return intval($item2 - $item1);
        }
        else {
          $item_diff = 12 - $item1;
          $item_diff += intval(($year_diff - 1) * 12);
          return $item_diff + $item2;
        }
        break;
      case 'days':
        $format = 'z';
        $item1 = date_format($date1, $format);
        $item2 = date_format($date2, $format);
        if ($year_diff == 0) {
          return intval($item2 - $item1);
        }
        else {
          $item_diff = date_days_in_year($date1) - $item1;
          for ($i = 1; $i < $year_diff; $i++) {
            date_modify($date1, '+1 year');
            $item_diff += date_days_in_year($date1);
          }
          return $item_diff + $item2;
        }
        break;
      case 'weeks':
        $week_diff = date_format($date2, 'W') - date_format($date1, 'W');
        $year_diff = date_format($date2, 'o') - date_format($date1, 'o');
        for ($i = 1; $i <= $year_diff; $i++) {
          date_modify($date1, '+1 year');
          $week_diff += date_iso_weeks_in_year($date1);
        }
        return $week_diff;
    }
  }
  return NULL;
}