Clock
Installation
composer require sonsofphp/clockUsage
SystemClock
<?php
use SonsOfPHP\Component\Clock\SystemClock;
$clock = new SystemClock();
$now = $clock->now(); // Returns a DateTimeImmutable objectFixedClock
<?php
use SonsOfPHP\Component\Clock\FixedClock;
$clock = new FixedClock();
$firstNow = $clock->now();
sleep(10);
$secondNow = $clock->now();
var_dump($firstNow === $secondNow); // true
// You can update the internal time at any time.
$clock->tick(); // Updates the internal clock to the curent time
$thridNow = $clock->now();
var_dump($firstNow === $secondNow); // still true
var_dump($firstNow === $thirdNow); // false
var_dump($thirdNow === $secondNow); // false
// You can also set the internal clock to whatever time you need it to be
$clock->tickTo('2022-04-20 04:20:00'); // format is "Y-m-d H:i:s"Last updated