Provides a simple abstraction of a clock, following the suggestion by Martin Fowler.
Package is available on Packagist, you can install it using Composer.
$ composer require ebene7/php-clock
Create a SystemClock object and simply use it! Object that will return the current time based on the given timezone
<?php
use E7\Clock\SystemClock;
$clock = new SystemClock();
$now = $clock->now();
It's also possible to set the timezone.
<?php
use DateTimeZone;
use E7\Clock\SystemClock;
$timezone = new DateTimeZone('Europe/Berlin');
$clock = new SystemClock(timezone);
$now = $clock->now();
The FrozenClock simplfies testing and it's also esy to use. The FrozenClock object always returns a fixed time object.
<?php
use DateTimeImmutable;
use E7\Clock\FrozenClock;
$now = new DateTimeImmutable();
$clock = new FrozenClock($now);
$now = $clock->now();
This project is inspired by lcobucci/clock
(originally licensed under MIT by Luís Cobucci).