DateTime
A libary to handle dates and times separately from one another., (*1)
, (*2)
Background
So far all the libraries around that help handling Dates and Times all extend
one of PHPs DateTime-Objects., (*3)
This library tries to address that by providing two separate Objects that
handle Dates and Times respectively. Internaly they do use PHPs
DateTimeImmutable-Object but that's not exposed to the outside., (*4)
So now you can use a Date-Object without having to worry about the
time-component and vice-versa., (*5)
Installation
This library is best installed using composer., (*6)
composer require datetime/datetime
Usage
Date
There is a Date
-Object as well as a DateInterval
-Object, (*7)
Easiest usage is like this:, (*8)
$date = new \DateTime\Date('2018-07-05');
echo $date->format('d. m. Y');
// 05. 07. 2018
echo $date->format('jS \o\f F Y H:i:s');
// 5th of July 2018 H:i:s
Another possibility would be to use it like this:, (*9)
$date = new \DateTime\Date('last wednesday of june 2018');
echo $date->format('d. m. Y');
// 27. 06. 2018
Or to use the DateInterval
-Object:, (*10)
$date1 = new \DateTime\Date('2018-07-05');
$date2 = new \DateTime\Date('2020-07-05');
$interval = $date1->diff($date2);
echo $interval->format('%d %m %y');
// 731 0 0
Time
There is also a Time
-Object as well as a TimeInterval
-Object, (*11)
Those can be used as follows:, (*12)
$time = new \DateTime\Time('12:23:34');
echo $time->format('H:i:s');
// 12:23:34
echo $time->format('jS \o\f F Y H:i:s');
// jS of F Y 12:23:34