Period

The Datepoint object

The Datepoint class is introduced in version 4.2.

A datepoint is a position in time expressed as a DateTimeImmutable object.

The Datepoint class is introduced to ease Datepoint manipulation. This class extends PHP’s DateTimeImmutable class by adding a new named constructor and several getter methods.

Named constructor

Datepoint::create

public Datepoint::create($datepoint): self

Returns a Datepoint 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 Datepoint object will use the current timezone.

examples

Using the $datepoint argument

use League\Period\Datepoint;

Datepoint::create('yesterday'); // returns new Datepoint('yesterday')
Datepoint::create('2018');      // returns new Datepoint('@2018')
Datepoint::create(2018);        // returns new Datepoint('@2018')
Datepoint::create(new DateTime('2018-10-15'));  // returns new Datepoint('2018-10-15')
Datepoint::create(new DateTimeImmutable('2018-10-15'));  // returns new Datepoint('2018-10-15')

Accessing calendar interval

Once you’ve got a Datepoint instantiated object, you can access a set of calendar type interval using the following methods.

public Datepoint::getSecond(): Period;
public Datepoint::getMinute(): Period
public Datepoint::getHour(): Period
public Datepoint::getDay(): Period
public Datepoint::getIsoWeek(): Period
public Datepoint::getMonth(): Period
public Datepoint::getQuarter(): Period
public Datepoint::getSemester(): Period
public Datepoint::getYear(): Period
public Datepoint::getIsoYear(): Period

For each a these methods a Period object is returned with:

Examples

use League\Period\Datepoint;

$datepoint = new Datepoint('2018-06-18 08:35:25');
$hour = $datepoint->getHour();
// new Period('2018-06-18 08:00:00', '2018-06-18 09:00:00');
$month = $datepoint->getMonth();
// new Period('2018-06-01 00:00:00', '2018-07-01 00:00:00');
$month->contains($datepoint); // true
$hour->contains($datepoint); // true
$month->contains($hour); // true