You are here

final class BillingPeriod in Commerce Recurring Framework 8

Represents a single billing period.

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

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

Hierarchy

Expanded class hierarchy of BillingPeriod

See also

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

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

19 files declare their use of BillingPeriod
BillingPeriodItem.php in src/Plugin/Field/FieldType/BillingPeriodItem.php
BillingPeriodItemTest.php in tests/src/Kernel/BillingPeriodItemTest.php
BillingPeriodTest.php in tests/src/Kernel/BillingPeriodTest.php
BillingScheduleInterface.php in src/Plugin/Commerce/BillingSchedule/BillingScheduleInterface.php
ChargeTest.php in tests/src/Kernel/ChargeTest.php

... See full list

File

src/BillingPeriod.php, line 20

Namespace

Drupal\commerce_recurring
View source
final class BillingPeriod {

  /**
   * The start date/time.
   *
   * @var \Drupal\Core\Datetime\DrupalDateTime
   */
  protected $startDate;

  /**
   * The end date/time.
   *
   * @var \Drupal\Core\Datetime\DrupalDateTime
   */
  protected $endDate;

  /**
   * Constructs a new BillingPeriod object.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $start_date
   *   The start date/time.
   * @param \Drupal\Core\Datetime\DrupalDateTime $end_date
   *   The end date/time.
   */
  public function __construct(DrupalDateTime $start_date, DrupalDateTime $end_date) {
    $this->startDate = $start_date;
    $this->endDate = $end_date;
  }

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

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

  /**
   * 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 \Drupal\Core\Datetime\DrupalDateTime $date
   *   The date/time.
   *
   * @return bool
   *   TRUE if the date/time is contained in the period, FALSE otherwise.
   */
  public function contains(DrupalDateTime $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
BillingPeriod::$endDate protected property The end date/time.
BillingPeriod::$startDate protected property The start date/time.
BillingPeriod::contains public function Checks whether the given date/time is contained in the period.
BillingPeriod::getDuration public function Gets the duration of the billing period, in seconds.
BillingPeriod::getEndDate public function Gets the end date/time.
BillingPeriod::getStartDate public function Gets the start date/time.
BillingPeriod::__construct public function Constructs a new BillingPeriod object.