You are here

protected function FixedReferenceDateInterval::findNextAppropriateDate in Recurring Time Period 8

Finds the next appropriate date after the start date.

Parameters

\DateTimeImmutable $start_date: The start date.

string $reference_date_string: A date string using the PHP date format 'Y-m-d'. The timezone will be assumed to be that of the $start_date.

\DateInterval $interval: The interval.

Return value

\DateTimeImmutable The end date.

1 call to FixedReferenceDateInterval::findNextAppropriateDate()
FixedReferenceDateInterval::calculateEnd in src/Plugin/RecurringPeriod/FixedReferenceDateInterval.php
Calculates the end date and time for the period.

File

src/Plugin/RecurringPeriod/FixedReferenceDateInterval.php, line 160

Class

FixedReferenceDateInterval
Provides a fixed date period.

Namespace

Drupal\recurring_period\Plugin\RecurringPeriod

Code

protected function findNextAppropriateDate(\DateTimeImmutable $start_date, $reference_date_string, \DateInterval $interval) {
  $reference_date = new \DateTimeImmutable($reference_date_string, $start_date
    ->getTimezone());
  $is_reference_date_in_future = $reference_date
    ->diff($start_date)->invert;
  if ($is_reference_date_in_future) {

    // The reference date is in the future, so rewind it until it precedes
    // the start date, then increase it by one interval unit to find the
    // next appropriate date.
    while ($reference_date
      ->diff($start_date)->invert == TRUE) {
      $reference_date = $reference_date
        ->sub($interval);
    }
    $reference_date = $reference_date
      ->add($interval);
  }
  else {

    // The reference date is in the past, so fast forward it until the next
    // increment beyond the start date to find the next appropriate date.
    while ($reference_date
      ->diff($start_date)->invert == FALSE) {
      $reference_date = $reference_date
        ->add($interval);
    }
  }
  return $reference_date;
}