Period

Comparing

You can compare different Period objects according to their datepoints or durations.

datepoint and duration conversions are done internally using the League\Period\datepoint and the League\Period\duration functions.

Using datepoints

Period::isBefore

public Period::isBefore(mixed $index): bool

Tells whether the current Period object datetime continuum is entirely before the specified $index.

Parameter

The $index argument can be another Period object or a datepoint.

Examples

use function League\Period\month;

$period = month(1983, 4);
$alt = month(1984, 4);

//test against another Period object
$period->isBefore($alt); //returns true;
$alt->isBefore($period); //return false;

//test againts a datepoint
$period->isBefore('1983-06-02'); //returns true
$period->isBefore('1982-06-02'); //returns false
$period->isBefore($period->getEndDate()); //returns true

Period::isAfter

public Period::isAfter(mixed $index): bool

Tells whether the current Period object datetime continuum is entirely after the specified $index.

Parameter

The $index argument can be another Period object or a datepoint.

Examples

use League\Period\Period;

$period = month(1983, 4);
$alt = month(1984, 4);

//test against another Period object
$alt->isAfter($period); //returns true;
$period->isAfter($alt); //return false;

//test againts a datepoint
$period->isAfter('1983-06-02'); //returns false
$period->isAfter('1982-06-02'); //returns true
$period->isAfter($period->getStartDate()); //returns false

Period::abuts

public Period::abuts(Period $interval): bool

A Period abuts if it starts immediately after, or ends immediately before the submitted Period without overlapping.

Examples

use League\Period\Period;

$period = month(2014, 3);
$alt = month(2014, 4);
$period->abuts($alt); //return true
//in this case $period->getEndDate() === $alt->getStartDate();

Period::overlaps

public Period::overlaps(Period $interval): bool

A Period overlaps another if they share some common part of their respective continuous portion of time without abutting.

Examples

$orig  = month('2014-03-15');
$alt   = month('2014-04-15');
$other = interval_after('2014-03-15', '3 WEEKS');

$orig->overlaps($alt);   //return false
$orig->overlaps($other); //return true
$alt->overlaps($other);  //return true

Period::equals

public Period::equals(Period $interval): bool

Tells whether two Period objects shares the same datepoints.

Examples

$orig  = month(2014, 3);
$alt   = month(2014, 4);
$other = interval_after('2014-03-01', '1 MONTH');

$orig->equals($alt);   //return false
$orig->equals($other); //return true

Period::contains

public Period::contains(mixed $index): bool

Parameter

The $index argument can be another Period object or a datepoint.

Examples

//comparing a datetime
$period = month(1983, 4);
$period->contains('1983-04-15');            //returns true;
$period->contains($period->getStartDate()); //returns true;
$period->contains($period->getEndDate());   //returns false;

//comparing two Period objects
$alt = interval_after('1983-04-12', '12 DAYS');
$period->contains($alt); //return true;
$alt->contains($period); //return false;

Period::diff

public Period::diff(Period $interval): array

This method returns the difference between two Period objects only if they actually do overlap. If they do not overlap or abut, then an Exception is thrown.

The difference is expressed as an array. The returned array always contains two values:

Examples

$orig = interval_after('2013-01-01', '1 MONTH');
$alt  = interval_after('2013-01-15', '7 DAYS');
list($first, $last) = $orig->diff($alt);
// $diff is an array containing 2 Period objects
$first->equals(new Period('2013-01-01', '2013-01-15')); // returns true
$last->equals(new Period('2013-01-23', '2013-02-01'));  // returns true
$first->isBefore($last); //return true;
//this is always true when two Period objects are present

Before getting the difference, make sure the Period objects, at least, overlap each other.

Period::intersect

public function intersect(Period $interval): Period

An Period overlaps another if it shares some common part of the datetime continuum. This method returns the amount of the overlap as a Period object, only if they actually do overlap. If they do not overlap, then an Period\Exception is thrown.

Before getting the intersection, make sure the Period objects, at least, overlap each other.

Examples

$interval = interval_after('2012-01-01', '2 MONTHS');
$alt_interval = interval_after('2012-01-15', '3 MONTHS');
$intersection = $interval->intersect($alt_interval);

Period::gap

public function gap(Period $interval): Period

A Period has a gap with another Period if there is a non-zero interval between them. This method returns the amount of the gap as a new Period object only if they do actually have a gap between them. If they overlap a Exception is thrown.

Before getting the gap, make sure the Period objects do not overlaps.

Examples

$interval = interval_after('2012-01-01', '2 MONTHS');
$alt_interval = interval_after('2013-01-15', '3 MONTHS');
$gap = $interval->gap($alt_interval);

Using durations

Sorting objects

public Period::durationCompare(Period $interval): int
public Period::durationGreaterThan(Period $interval): bool
public Period::durationLessThan(Period $interval): bool
public Period::durationEquals(Period $interval): bool

The method Period::durationCompare compares two Period objects according to their duration. The method returns:

To ease the method usage you can rely on the following proxy methods:

Examples

$orig = interval_after('2012-01-01', '1 MONTH');
$alt = interval_after('2012-01-01', '1 WEEK');
$other = interval_after('2013-01-01', '1 MONTH');

$orig->durationCompare($alt);     //return 1
$orig->durationGreaterThan($alt); //return true
$orig->durationLessThan($alt);    //return false

$alt->durationCompare($other);     //return -1
$alt->durationLessThan($other);    //return true
$alt->durationGreaterThan($other); //return false

$orig->durationCompare($other);   //return 0
$orig->durationEquals($other);    //return true
$orig->equals($other);       //return false
//the duration between $orig and $other are equals but not the datepoints!!

Returning the duration differences

public Period::dateIntervalDiff(Period $interval): DateInterval
public Period::timestampIntervalDiff(Period $interval): float

Returns the duration difference between two Period objects using a DateInterval object or expressed in seconds.

Examples

use League\Period\Period;

$interval = semester(2012, 1);
$alt_interval = iso_week(2012, 4);
$diff = $interval->dateIntervalDiff($alt_interval);
// $diff is a DateInterval object
$diff_as_seconds = $interval->timestampIntervalDiff($alt_interval);
//$diff_as_seconds represents the interval expressed in seconds