You are here

public static function CalendarHelper::difference in Calendar 8

Computes difference between two days using a given measure.

Parameters

\DateTime $start_date: The start date.

\DateTime $stop_date: 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 between the 2 dates in the given measure.

1 call to CalendarHelper::difference()
Calendar::calendarBuildWeekDay in src/Plugin/views/style/Calendar.php
Fill in the selected day info into the event buckets.

File

src/CalendarHelper.php, line 114

Class

CalendarHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\calendar

Code

public static function difference(\DateTime $start_date, \DateTime $stop_date, $measure = 'seconds', $absolute = TRUE) {

  // Create cloned objects or original dates will be impacted by the
  // date_modify() operations done in this code.
  $date1 = clone $start_date;
  $date2 = clone $stop_date;
  if (is_object($date1) && is_object($date2)) {
    $diff = $date2
      ->format('U') - $date1
      ->format('U');
    if ($diff == 0) {
      return 0;
    }
    elseif ($diff < 0 && $absolute) {

      // Make sure $date1 is the smaller date.
      $temp = $date2;
      $date2 = $date1;
      $date1 = $temp;
      $diff = $date2
        ->format('U') - $date1
        ->format('U');
    }
    $year_diff = intval($date2
      ->format('Y') - $date1
      ->format('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 = $date1
          ->format($format);
        $item2 = $date2
          ->format($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 = $date1
          ->format($format);
        $item2 = $date2
          ->format($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++) {
            $date1
              ->modify('-1 year');

            // @todo self::daysInYear() throws a warning when used with a
            // \DateTime object. See https://www.drupal.org/node/2596043
            // $item_diff -= self::daysInYear($date1);
            $item_diff -= 365;
          }

          // Return $item_diff - (self::daysInYear($date2) - $item2);.
          return $item_diff - (365 - $item2);
        }
        else {

          // @todo self::daysInYear() throws a warning when used with a
          // \DateTime object. See https://www.drupal.org/node/2596043
          // $item_diff = self::daysInYear($date1) - $item1;
          $item_diff = 365 - $item1;
          for ($i = 1; $i < $year_diff; $i++) {
            $date1
              ->modify('+1 year');

            // $item_diff += self::daysInYear($date1);
            $item_diff += 365;
          }
          return $item_diff + $item2;
        }
        break;
      case 'weeks':
        $week_diff = $date2
          ->format('W') - $date1
          ->format('W');
        $year_diff = $date2
          ->format('o') - $date1
          ->format('o');
        $sign = $year_diff < 0 ? -1 : 1;
        for ($i = 1; $i <= abs($year_diff); $i++) {
          $date1
            ->modify(($sign > 0 ? '+' : '-') . '1 year');
          $week_diff += self::isoWeeksInYear($date1) * $sign;
        }
        return $week_diff;
    }
  }
  return NULL;
}