The Datepoint class
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:
- a
TypeErrorif the submitted parameter have the wrong type.
parameters
$datepointcan be:- a
DateTimeInterfaceimplementing object - a string parsable by the
DateTimeconstructor. - an integer interpreted as a timestamp.
- a
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 method signature.
public function method(string $boundaryType = Period::INCLUDE_START_EXCLUDE_END): Period
where method is one of the following date time span:
Datepoint::getSecondDatepoint::getMinuteDatepoint::getHourDatepoint::getDayDatepoint::getIsoWeekDatepoint::getMonthDatepoint::getQuarterDatepoint::getSemesterDatepoint::getYearDatepoint::getIsoYear
For each a these methods a Period object is returned with:
- the
Period::INCLUDE_START_EXCLUDE_ENDboundary type by default unless changed using the$boundaryTypeargument; - the starting datepoint represents the beginning of the current datepoint calendar interval;
- the duration associated with the given calendar interval;
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(Period::INCLUDE_ALL);
echo $month->format('Y-m-d');
// [2018-06-01, 2018-07-01 00:00:00];
$month->contains($datepoint); // true
$hour->contains($datepoint); // true
$month->contains($hour); // true
Relational method against interval
A datepoint can also be evaluated in relation to a given interval.
The following methods all share the same signature:
public function method(Period $interval): bool
where method is one of the basic relation between a datepoint and an interval.
Datepoint::isBeforeDatepoint::bordersOnStartDatepoint::isStartingDatepoint::isDuringDatepoint::isEndingDatepoint::bordersOnEndDatepoint::abutsDatepoint::isAfter
Examples
use League\Period\Datepoint;
use League\Period\Period;
$datepoint = Datepoint::create('2018-01-18 10:00:00');
$datepoint->isBorderingOnStart(
Period::after($datepoint, '3 minutes', Period::EXCLUDE_START_INCLUDE_END)
); // true
$datepoint->isBorderingOnStart(
Period::after($datepoint, '3 minutes', Period::INCLUDE_ALL)
); // false
$datepoint->isAfter(
Period::before('2018-01-13 23:34:28', '3 minutes', Period::INCLUDE_START_EXCLUDE_END)
); // true