Provides an immutable Clock implementation for PHP
You can install the package via composer:
composer require ghostwriter/clock
You can also star (🌟) this repo to find it easier later.
<?php
// Internally uses SystemClock::new(new DateTimeZone(date_default_timezone_get()));
date_default_timezone_set('America/Los_Angeles');
$systemClock = SystemClock::new();
$systemClock->now(); // DateTimeImmutable
$systemClock->now()->getTimezone()->getName(); // America/Los_Angeles
date_default_timezone_set('America/New_York');
$systemClock = SystemClock::new();
$systemClock->now(); // DateTimeImmutable
$systemClock->now()->getTimezone()->getName(); // America/New_York
$localizedClock = LocalizedClock::new();
$localizedClock->now(); // DateTimeImmutable
$localizedClock->now()->getTimezone()->getName(); // UTC
$localizedClock = LocalizedClock::new(new DateTimeZone('Africa/Addis_Ababa'));
$localizedClock->now(); // DateTimeImmutable
$localizedClock->now()->getTimezone()->getName(); // Africa/Addis_Ababa
$frozenClock = FrozenClock::new(new DateTimeImmutable('now', new DateTimeZone('UTC')));
$frozenClock->now(); // DateTimeImmutable
$frozenClock->now()->getTimezone()->getName(); // UTC
interface ClockInterface
{
public function freeze(): FrozenClockInterface;
public function now(): DateTimeImmutable;
}
interface FrozenClockInterface extends ClockInterface
{
public static function new(DateTimeImmutable $dateTimeImmutable): self;
}
interface LocalizedClockInterface extends ClockInterface
{
public static function new(DateTimeZone $dateTimeZone): self;
}
interface SystemClockInterface extends ClockInterface
{
public static function new(): self;
}
Please see CHANGELOG.md for more information what has changed recently.
If you discover any security related issues, please email nathanael.esayeas@protonmail.com
or create a Security Advisory instead of using the issue tracker.
The BSD-3-Clause. Please see License File for more information.