Skip to content

Getting Started

Clock is a PSR-20 compliant clock abstraction for PHP that provides multiple implementations for different use cases, including Carbon, DateTime, and specialized testing clocks.

Requires PHP 8.4+

Terminal window
composer require cline/clock

The package auto-registers with Laravel via package discovery.

use Cline\Clock\Clocks\CarbonImmutableClock;
use Cline\Clock\Clocks\FrozenClock;
use DateTimeImmutable;
// Production: Get current time
$clock = new CarbonImmutableClock();
$now = $clock->now(); // DateTimeImmutable
// Testing: Fixed time for deterministic tests
$frozen = new FrozenClock(new DateTimeImmutable('2025-01-15 12:00:00'));
$frozen->now(); // Always returns 2025-01-15 12:00:00

Use the clock() helper for quick access:

use function Cline\Clock\clock;
$now = clock()->now(); // Uses CarbonImmutableClock by default
// With timezone
$clock = clock(timezone: new DateTimeZone('America/New_York'));
use Cline\Clock\Facades\Clock;
$now = Clock::now();
ClockUse Case
CarbonImmutableClockDefault for Laravel apps
CarbonClockMutable Carbon instances
DateTimeImmutableClockNative PHP without dependencies
DateTimeClockNative mutable DateTime
UtcClockAlways UTC timezone
FrozenClockFixed time for testing
MockClockMutable testing clock
SequenceClockPredetermined time sequence
TickClockManual time advancement
OffsetClockTime offset decorator