1 date.class.inc public BackdropDateTime::difference($date2_in, $measure = 'seconds', $absolute = TRUE)

Computes difference between two days using a given measure.

Parameters

object $date2_in: The stop date.

string $measure: (optional) A granularity date part. Defaults to 'seconds'.

bool $absolute: (optional) Indicate whether the absolute value of the difference should be returned or if the sign should be retained. Defaults to TRUE.

Return value

int: The difference in the measurement of the $measure parameter.

File

core/includes/date.class.inc, line 858

Class

BackdropDateTime
Extends PHP DateTime class for use with Backdrop.

Code

public function difference($date2_in, $measure = 'seconds', $absolute = TRUE) {
  // Create cloned objects or original dates will be impacted by the
  // date_modify() operations done in this code.
  $date1 = clone($this);
  $date2 = clone($date2_in);
  if (is_object($date1) && is_object($date2)) {
    $diff = date_format($date2, 'U') - date_format($date1, 'U');
    if ($diff == 0) {
      return 0;
    }
    elseif ($diff < 0 && $absolute) {
      // 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);
        }
        elseif ($year_diff < 0) {
          $item_diff = 0 - $item1;
          $item_diff -= intval((abs($year_diff) - 1) * 12);
          return $item_diff - (12 - $item2);
        }
        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);
        }
        elseif ($year_diff < 0) {
          $item_diff = 0 - $item1;
          for ($i = 1; $i < abs($year_diff); $i++) {
            date_modify($date1, '-1 year');
            $item_diff -= date_days_in_year($date1);
          }
          return $item_diff - (date_days_in_year($date2) - $item2);
        }
        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');

        $sign = ($year_diff < 0) ? -1 : 1;

        for ($i = 1; $i <= abs($year_diff); $i++) {
          date_modify($date1, (($sign > 0) ? '+' : '-') . '1 year');
          $week_diff += (date_iso_weeks_in_year($date1) * $sign);
        }
        return $week_diff;
    }
  }
  return NULL;
}