Author Latest Version Software License Total Downloads

Period is PHP’s missing time range API. Based on ideas from Resolving Feature Envy in the Domain by Mathias Verraes, this package extends the concept to cover all basic operations regarding time ranges.

In your code, you will always have to typehint against the League\Period\Period class directly because it is a immutable value object class marked as final and the library does not provide an interface.

Accessing the interval properties

use League\Period\Period;

$interval = Period::fromNotation('!Y-m-d', '[2014-10-03 08:12:37,2014-10-03 08:12:37)');
$start = $interval->startDate();             //returns a DateTimeImmutable
$end = $interval->endDate();                 //returns a DateTimeImmutable
$duration = $interval->dateInterval();       //returns a DateInterval object
$duration2 = $interval->timestampInterval(); //returns the duration in seconds
echo $interval->toIso8601(); //displays '2014-10-03T08:12:37Z/2014-10-03T09:12:37Z'

Learn more about how this all works in the basic usage.

Iterate over the interval

A simple example on how to get all the days from a selected month.

foreach (Period::fromMonth(2014, 10)->toDatePeriod(new DateInterval('P1D')) as $day) {

To help easily instantiate your time range and manipulating it, the package comes bundle with named constructors and helper classes.

Comparing intervals

$period = Period::after(new DateTime('2014-01-01'), DateInterval::createFromDateString('1 MONTH'), Period::INCLUDE_ALL);
$altPeriod = Period::after(new DateTimeImmutable('2014-01-01'), new DateInterval('P1M'), Period::EXCLUDE_ALL);
$period->durationEquals($altPeriod); //returns true
$period->equals($altPeriod); //returns false
$period->contains($altPeriod); //returns true
$altPeriod->contains($period); //return false
$period->contains('2014-01-10'); //returns true
DatePoint::fromDateString('2014-02-10')->isDuring($period); //returns false

The class comes with other ways to compare time ranges based on their duration and/or their datepoints.
Datepoint decorates the DateTimeImmutable object and offers more methods.

Modifying interval

$period = Period::after(new DateTimeImmutable('2014-01-01'), DateInterval::createFromDateString('1 WEEK'));
$altPeriod = $period->endingOn('2014-02-03');
$period->contains($altPeriod); //return false;
$altPeriod->durationGreaterThan($period); //return true;

Period is an immutable value object. Any changes to the object returns a new object. The class has more modifying methods.

Accessing all gaps between intervals

$sequence = new Sequence(
    Period::fromNotation('!Y-m-d', '[2018-01-01,2018-01-31)'),
    Period::fromNotation('!Y-m-d', '[2017-01-01,2017-01-31)'),
    Period::fromNotation('!Y-m-d', '[2020-01-01,2020-01-31)')
$gaps = $sequence->gaps(); // a new Sequence object
count($gaps); // 2

Sequence is a Period container and collection. The class has more methods.