The Themosis datetime component provides a Clock interface around PHP date and time functions.
Install the library using Composer:
composer require themosis/datetimeThe library provides 2 interfaces to deal with date and time:
- The
Clockinterface - The
MutableClockinterface
As the name suggests, everytime the Clock interface is referenced, you actually get an immutable clock instance.
The MutableClock interface provides an additional method to let you modify the current time.
The library comes with a default implementation of the Clock and MutableClock interfaces.
You can create a new clock by using the SystemClock class:
<?phpuseThemosis\Components\Datetime\SystemClock;
$clock = newSystemClock();By default, a clock without any parameters will create a clock with "now" as the current time. It also sets the current timezone to "UTC".
To retrieve the clock current time, use the currentTime() method.
The method returns a DateTimeImmutable instance you can work with.
The currentTime() method is returning the DateTimeImmutable instance with its value generated at instantiation.
<?phpuseThemosis\Components\Datetime\SystemClock;
$clock = newSystemClock();
$datetime = $clock->currentTime();To retrieve the clock timezone, use the timezone() method.
The method returns a DateTimeZone instance.
<?phpuseThemosis\Components\Datetime\SystemClock;
$clock = newSystemClock();
$timezone = $clock->timezone();If you need to retrieve the current "now" time from the clock, use the now() method:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42');
$clock = newSystemClock($aDate);
$currentTime = $clock->currentTime();
// 2020-01-01 11:05:42$now = $clock->now();The above $now variable is a new Clock instance. To access its value, use the currentTime() method.
The clock instance accepts 2 parameters in its constructor:
- The
currentTimeas aDateTimeImmutablevalue. - The
timezoneas aDateTimeZonevalue.
You can specify the clock to represent a specific moment in time like so:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42');
$clock = newSystemClock($aDate);If no timezone is provided as a second parameter, the clock instance will set the timezone to "UTC".
In the case a timezone is specified inside the given DateTimeImmutable value, the clock instance will respect and set its timezone accordingly:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42', newDateTimeZone('Europe/Brussels'));
$clock = newSystemClock($aDate);
$clock
->timezone()
->getName();
// "Europe/Brussels"You can pass a timezone parameter to the clock constructor and it will be used as the clock timezone, overriding the timezone passed to the datetime parameter:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42', newDateTimeZone('Europe/Brussels'));
$aTimezone = newDateTimeZone( 'America/New_York' );
$clock = newSystemClock( $aDate, $aTimezone );
$clock
->timezone()
->getName();
// "America/New_York"$clock
->currentTime()
->getTimezone()
->getName();
// "America/New_York"The given
DateTimeImmutableinstance timezone is also updated with the one provided in the constructor.
You can check if the clock is currently in the past using the isPast() method:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42');
$clock = newSystemClock($aDate);
$clock->isPast();
// trueYou can check if the clock is currently in the future using the isFuture() method:
<?phpuseThemosis\Components\Datetime\SystemClock;
$aDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '3506-04-21 08:23:08');
$clock = newSystemClock( $aDate );
$clock->isFuture();
// trueThe SystemClock implements the MutableClock interface which extends the Clock interface.
The SystemClock can be used as an immutable and mutable clock.
You can modify the clock current time at runtime using the setCurrentTime() method:
<?phpuseThemosis\Components\Datetime\SystemClock;
$clock = newSystemClock();
$clock->setCurrentTime(
DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 11:05:42')
);You can modify the clock timezone at runtime using the setTimezone() method:
<?phpuseThemosis\Components\Datetime\SystemClock;
$clock = newSystemClock();
$clock->setTimezone(newDateTimeZone('Europe/Brussels'));