Period

Definition and Argument

Concepts

Arguments

Since this package relies heavily on DateTimeImmutable and DateInterval objects and because it is sometimes complicated to get your hands on such objects the package comes bundled with two simple functions that are used throughout the library to ensure typesafety. These functions are defined under the League\Period namespace.

datepoint

function League\Period\datepoint(mixed $datepoint): DateTimeImmutable;

Returns a DateTimeImmutable object or throws:

parameters

Because we are using PHP's parser, values exceeding ranges will be added to their parent values.

If no timezone information is given, the returned DateTimeImmutable object will use the current timezone.

examples

Using the $datepoint argument

use function League\Period\datepoint;

datepoint('yesterday'); // returns new DateTimeImmutable('yesterday')
datepoint('2018');      // returns new DateTimeImmutable('@2018')
datepoint(2018);        // returns new DateTimeImmutable('@2018')
datepoint(new DateTime('2018-10-15'));  // returns new DateTimeImmutable('2018-10-15')
datepoint(new DateTimeImmutable('2018-10-15'));  // returns new DateTimeImmutable('2018-10-15')

duration

function League\Period\duration(mixed $duration): DateInterval;

Converts its single input into a DateInterval object or throws a TypeError otherwise.

parameter

$duration can be:

WARNING: When the string is not parsable by DateInterval::createFromDateString a DateInterval object representing the 0 interval is returned as described in PHP bug #50020.

Examples

use function League\Period\datepoint;
use function League\Period\duration;

$daterange = new DatePeriod(
    datepoint(123456789),
    duration(600),
    datepoint(new DateTime())
);

//returns the same object as if you had written

$daterange = new DatePeriod(
    new DateTimeImmutable('@123456789'),
    new DateInterval('PT600S'),
    new DateTimeImmutable()
);