Symfony Clock decouples applications from the system clock.
composer require symfony/clockuseSymfony\Component\Clock\NativeClock;
useSymfony\Component\Clock\ClockInterface;
class MyClockSensitiveClass
{
publicfunction__construct(
privateClockInterface$clock,
) {
// Only if you need to force a timezone://$this->clock = $clock->withTimeZone('UTC');
}
publicfunctiondoSomething()
{
$now = $this->clock->now();
// [...] do something with $now, which is a \DateTimeImmutable object$this->clock->sleep(2.5); // Pause execution for 2.5 seconds
}
}
$clock = newNativeClock();
$service = newMyClockSensitiveClass($clock);
$service->doSomething();This package is looking for a backer.
Help Symfony by sponsoring its development!