You are here

class Period in Recurring Time Period 8

Represents a period of time with specific start and end dates.

Examples:

  • Oct 14th 14:56:20 - Nov 14th 14:56:20
  • May 1st 00:00:00 - June 1st 00:00:00
  • June 1st 00:00:00 - July 1st 00:00:00

Periods are contiguous and represent half-open ranges (the end date is not included in the duration).

Hierarchy

  • class \Drupal\recurring_period\Datetime\Period

Expanded class hierarchy of Period

See also

http://wrschneider.github.io/2014/01/07/time-intervals-and-other-ranges-...

\Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule\BillingScheduleInterface

2 files declare their use of Period
RecurringPeriodBase.php in src/Plugin/RecurringPeriod/RecurringPeriodBase.php
RecurringPeriodInterface.php in src/Plugin/RecurringPeriod/RecurringPeriodInterface.php

File

src/DateTime/Period.php, line 20

Namespace

Drupal\recurring_period\Datetime
View source
class Period {

  /**
   * The start date/time.
   *
   * @var \DateTimeImmutable
   */
  protected $startDate;

  /**
   * The end date/time.
   *
   * @var \DateTimeImmutable
   */
  protected $endDate;

  /**
   * The optional label for the period.
   */
  protected $label = '';

  /**
   * Constructs a new Period object.
   *
   * @param \DateTimeImmutable $start_date
   *   The start date/time.
   * @param \DateTimeImmutable $end_date
   *   The end date/time.
   * @param string|null $label
   *   (optional) The label.
   */
  public function __construct(\DateTimeImmutable $start_date, \DateTimeImmutable $end_date, $label = '') {
    $this->startDate = $start_date;
    $this->endDate = $end_date;
    $this->label = $label;
  }

  /**
   * Creates an entity from this period.
   *
   * This requires the entity class for the specified entity type to use
   * \Drupal\recurring_period\Entity\PeriodEntityTrait, or have compatible
   * entity keys.
   *
   * @param string $entity_type_id
   *   The entity type ID of the entity to create.
   * @param array $values
   *   An array of initial values for the new entity. If this contains a value
   *   for the label field, it will be taken over any label from the Period
   *   object.
   *
   * @return \Drupal\Core\Entity\EntityBase
   *   The unsaved entity.
   */
  public function toEntity($entity_type_id, $values = []) {
    $entity_type = \Drupal::entityTypeManager()
      ->getDefinition($entity_type_id);
    if ($entity_type
      ->hasKey('date_range')) {
      $values += [
        $entity_type
          ->getKey('date_range') => [
          'value' => $this
            ->getStartDate()
            ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
          'end_value' => $this
            ->getEndDate()
            ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
        ],
      ];
    }
    else {
      $values += [
        $entity_type
          ->getKey('start_date') => $this
          ->getStartDate()
          ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
        $entity_type
          ->getKey('end_date') => $this
          ->getEndDate()
          ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
      ];
    }
    if ($entity_type
      ->hasKey('label')) {
      $values += [
        $entity_type
          ->getKey('label') => $this
          ->getLabel(),
      ];
    }
    return \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->create($values);
  }

  /**
   * Gets the start date/time.
   *
   * @return \DateTimeImmutable
   *   The start date/time.
   */
  public function getStartDate() {
    return $this->startDate;
  }

  /**
   * Gets the end date/time.
   *
   * @return \DateTimeImmutable
   *   The end date/time.
   */
  public function getEndDate() {
    return $this->endDate;
  }

  /**
   * Gets the label.
   *
   * @return string
   *   The label.
   */
  public function getLabel() {
    return $this->label;
  }

  /**
   * Gets the duration of the billing period, in seconds.
   *
   * @return int
   *   The duration.
   */
  public function getDuration() {
    return $this->endDate
      ->format('U') - $this->startDate
      ->format('U');
  }

  /**
   * Checks whether the given date/time is contained in the period.
   *
   * @param \\DateTimeImmutable $date
   *   The date/time.
   *
   * @return bool
   *   TRUE if the date/time is contained in the period, FALSE otherwise.
   */
  public function contains(\DateTimeImmutable $date) {

    // Unlike DateTime, DrupalDateTime objects can't be compared directly.
    $timestamp = $date
      ->format('U');
    $starts = $this->startDate
      ->format('U');
    $ends = $this->endDate
      ->format('U');
    return $timestamp >= $starts && $timestamp < $ends;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Period::$endDate protected property The end date/time.
Period::$label protected property The optional label for the period.
Period::$startDate protected property The start date/time.
Period::contains public function Checks whether the given date/time is contained in the period.
Period::getDuration public function Gets the duration of the billing period, in seconds.
Period::getEndDate public function Gets the end date/time.
Period::getLabel public function Gets the label.
Period::getStartDate public function Gets the start date/time.
Period::toEntity public function Creates an entity from this period.
Period::__construct public function Constructs a new Period object.