PHPackages                             php-architecture-kit/clock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-architecture-kit/clock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

php-architecture-kit/clock
==========================

Another Clock implementation but if you allow the PHP Architecture library to be used on your domains, then you may prefer `new SystemClock` from that library rather than any other.

1.0.1(1mo ago)191MITPHPPHP ^7.4 || ^8.0

Since Feb 11Pushed 1mo agoCompare

[ Source](https://github.com/php-architecture-kit/clock)[ Packagist](https://packagist.org/packages/php-architecture-kit/clock)[ Docs](https://github.com/php-architecture-kit/clock)[ RSS](/packages/php-architecture-kit-clock/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (3)Used By (1)

php-architecture-kit/clock
==========================

[](#php-architecture-kitclock)

PSR-20 Clock implementations for PHP applications. Provides testable time abstractions for domain-driven design and clean architecture.

Features
--------

[](#features)

- **PSR-20 compliant** - Implements `Psr\Clock\ClockInterface`
- **Testable** - `FrozenClock` for deterministic unit tests
- **Timezone-aware** - `LocalizedClock` for specific timezones
- **Zero dependencies** - Only requires `psr/clock`
- **PHP 7.4+** - Compatible with legacy and modern PHP

Installation
------------

[](#installation)

```
composer require php-architecture-kit/clock
```

Quick Start
-----------

[](#quick-start)

```
use PhpArchitecture\Clock\SystemClock;
use PhpArchitecture\Clock\FrozenClock;
use PhpArchitecture\Clock\LocalizedClock;

// Production: Use system clock
$clock = new SystemClock();
$now = $clock->now(); // DateTimeImmutable

// Testing: Use frozen clock
$clock = FrozenClock::at(new \DateTimeImmutable('2024-06-15 12:00:00'));
$now = $clock->now(); // Always returns '2024-06-15 12:00:00'

// Timezone-specific: Use localized clock
$clock = new LocalizedClock(new \DateTimeZone('Europe/Warsaw'));
$now = $clock->now(); // DateTimeImmutable in Warsaw timezone

// UTC shortcut
$clock = LocalizedClock::utc();
```

Clock Implementations
---------------------

[](#clock-implementations)

### SystemClock

[](#systemclock)

Returns the current system time. Use in production code.

```
$clock = new SystemClock();
$now = $clock->now(); // Current time
```

### FrozenClock

[](#frozenclock)

Returns a fixed time. Ideal for unit testing.

```
// Freeze at specific time
$clock = FrozenClock::at(new \DateTimeImmutable('2024-01-01 00:00:00'));

// Freeze at current time
$clock = FrozenClock::fromNow();

// Time never changes
$clock->now(); // Always the same
usleep(10000);
$clock->now(); // Still the same
```

### LocalizedClock

[](#localizedclock)

Returns current time in a specific timezone.

```
// Any timezone
$clock = new LocalizedClock(new \DateTimeZone('America/New_York'));

// UTC shortcut
$clock = LocalizedClock::utc();

$now = $clock->now();
echo $now->getTimezone()->getName(); // 'America/New_York' or 'UTC'
```

Usage in Domain Services
------------------------

[](#usage-in-domain-services)

Inject `ClockInterface` instead of calling `new \DateTimeImmutable()` directly:

```
use Psr\Clock\ClockInterface;

class OrderService
{
    public function __construct(
        private ClockInterface $clock
    ) {}

    public function createOrder(array $items): Order
    {
        return new Order(
            items: $items,
            createdAt: $this->clock->now()
        );
    }
}
```

### Production Configuration

[](#production-configuration)

```
// Symfony
services:
    Psr\Clock\ClockInterface:
        class: PhpArchitecture\Clock\SystemClock

// Laravel
$this->app->bind(ClockInterface::class, SystemClock::class);
```

### Testing

[](#testing)

```
class OrderServiceTest extends TestCase
{
    public function testOrderCreatedWithCorrectTimestamp(): void
    {
        $fixedTime = new \DateTimeImmutable('2024-06-15 12:00:00');
        $clock = FrozenClock::at($fixedTime);

        $service = new OrderService($clock);
        $order = $service->createOrder(['item1', 'item2']);

        $this->assertEquals($fixedTime, $order->getCreatedAt());
    }
}
```

Comparison
----------

[](#comparison)

ClockUse CaseTime Changes`SystemClock`ProductionYes`FrozenClock`Unit testsNo`LocalizedClock`Timezone-specific appsYesAPI Reference
-------------

[](#api-reference)

### SystemClock

[](#systemclock-1)

MethodDescription`now(): DateTimeImmutable`Returns current system time### FrozenClock

[](#frozenclock-1)

MethodDescription`__construct(DateTimeImmutable $frozenAt)`Create with fixed time`at(DateTimeImmutable $frozenAt): self`Factory: create with fixed time`fromNow(): self`Factory: freeze current time`now(): DateTimeImmutable`Returns the frozen time### LocalizedClock

[](#localizedclock-1)

MethodDescription`__construct(DateTimeZone $timeZone)`Create with timezone`utc(): self`Factory: create UTC clock`now(): DateTimeImmutable`Returns current time in timezoneTesting
-------

[](#testing-1)

Package is tested with PHPUnit in the [php-architecture-kit/workspace](https://github.com/php-architecture-kit/workspace) project.

License
-------

[](#license)

MIT

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance88

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~83 days

Total

2

Last Release

59d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/145db325ca53baabce2d955faebe43d56bc4f3122b9ae463d41ee0e5da487cea?d=identicon)[patrykbaszak](/maintainers/patrykbaszak)

---

Top Contributors

[![patrykbaszak](https://avatars.githubusercontent.com/u/66377724?v=4)](https://github.com/patrykbaszak "patrykbaszak (7 commits)")

---

Tags

clockdddframework agnostic

### Embed Badge

![Health badge](/badges/php-architecture-kit-clock/health.svg)

```
[![Health](https://phpackages.com/badges/php-architecture-kit-clock/health.svg)](https://phpackages.com/packages/php-architecture-kit-clock)
```

###  Alternatives

[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k53](/packages/ecotone-ecotone)[symfony/clock

Decouples applications from the system clock

436205.7M390](/packages/symfony-clock)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

378604.0k104](/packages/flow-php-etl)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[beste/clock

A collection of Clock implementations

7427.5M29](/packages/beste-clock)[ergebnis/clock

Provides abstractions of a clock.

301.2M](/packages/ergebnis-clock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
