You are here

public function IntervalItem::applyInterval in Interval Field 8

Applies an interval to a date object.

Parameters

\DateTime $date: A DateTime object to which the interval needs to be applied

bool $limit: When calling the interval apply function with months or a month multiplier, keep the date in the last day of the month if this was exceeded. Example, with $limit set to TRUE, January 31st +1 month will result in February 28th.

Throws

\Drupal\interval\InvalidIntervalException

Overrides IntervalItemInterface::applyInterval

File

src/Plugin/Field/FieldType/IntervalItem.php, line 116

Class

IntervalItem
Provides a data type plugin for an interval item.

Namespace

Drupal\interval\Plugin\Field\FieldType

Code

public function applyInterval(\DateTime $date, $limit = FALSE) {
  try {
    $old_date = clone $date;
    $datetime = $this
      ->buildPHPString();
    $date
      ->modify($datetime);
    $configuration = $this
      ->getIntervalPlugin();
    if ($limit && $configuration['php'] == 'months') {
      $date_interval = $date
        ->diff($old_date);
      if ($date_interval->d != 0) {
        $date
          ->modify("last day of last month");
      }
    }
  } catch (\Exception $e) {
    throw new InvalidIntervalException($e
      ->getMessage(), 0, $e, $date, $this);
  }
}