You are here

public function Interval::subtract in Commerce Core 8.2

Subtracts the interval from the given date.

Parameters

\Drupal\Core\Datetime\DrupalDateTime $date: The date.

Return value

\Drupal\Core\Datetime\DrupalDateTime The new date.

File

src/Interval.php, line 116

Class

Interval
Provides a value object for intervals (1 month, 14 days, etc).

Namespace

Drupal\commerce

Code

public function subtract(DrupalDateTime $date) : DrupalDateTime {

  /** @var \Drupal\Core\Datetime\DrupalDateTime $new_date */
  $new_date = clone $date;
  $new_date
    ->modify('-' . $this
    ->__toString());

  // Mar 31st - 1 month should Feb 28th, not Mar 3rd.
  if ($this->unit == 'month' && $new_date
    ->format('d') !== $date
    ->format('d')) {
    $new_date
      ->modify('last day of previous month');
  }
  return $new_date;
}