ebene7/php-clock

There is no license information available for the latest version (v1.0.0) of this package.

E7 PHP clock

v1.0.0 2020-07-09 20:02 UTC

This package is auto-updated.

Last update: 2024-10-22 14:16:17 UTC


README

Provides a simple abstraction of a clock, following the suggestion by Martin Fowler.

Installation

Package is available on Packagist, you can install it using Composer.

$ composer require ebene7/php-clock

Basic usage

SystemClock

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();

FrozenClock

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();

Credits

This project is inspired by lcobucci/clock (originally licensed under MIT by Luís Cobucci).