PHPackages                             baldinof/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. baldinof/clock

ActiveLibrary

baldinof/clock
==============

Simple clock abstraction

1.0.1(5y ago)330.5k↓39.5%1MITPHPPHP &gt;=7.4

Since Sep 22Pushed 4y agoCompare

[ Source](https://github.com/Baldinof/clock)[ Packagist](https://packagist.org/packages/baldinof/clock)[ Docs](https://github.com/baldinof/clock)[ GitHub Sponsors](https://github.com/Baldinof)[ RSS](/packages/baldinof-clock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

PHP Clock abstraction
=====================

[](#php-clock-abstraction)

[![Latest Version on Packagist](https://camo.githubusercontent.com/38f5de47e80aebe07b3bd8527513d2da1ab71deddb845ec274400535eedf63bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62616c64696e6f662f636c6f636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/baldinof/clock)[![GitHub Tests Action Status](https://camo.githubusercontent.com/a57ea0afd651aec24989302f4f2b1c999e594e6da054aaea6b830a20da9e2627/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f62616c64696e6f662f636c6f636b2f5465737473)](https://github.com/baldinof/clock/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/7e78197376a3b46760a90f482c8c2e74710c5c0ae52c8d526c583af5f535724f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62616c64696e6f662f636c6f636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/baldinof/clock)

Get current time in a static *and* testable way.

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

[](#installation)

You can install the package via composer:

```
composer require baldinof/clock
```

Static usage
------------

[](#static-usage)

```
use Baldinof\Clock\Clock;

$now = Clock::now();

assert($now instanceof \DateTimeImmutable);
```

### Why static calls instead of dependency injection?

[](#why-static-calls-instead-of-dependency-injection)

Static calls are very convenient when defining models:

```
use Baldinof\Clock\Clock;
use Ramsey\Uuid\Uuid;

final class User
{
    private $id;
    private $createdAt;
    private $name;

    public function __construct(string $name)
    {
        $this->id = Uuid::uuid4();
        $this->createdAt = Clock::now();
        $this->name = $name;
    }

    // Getters...
}
```

Testing
-------

[](#testing)

You can use the `FrozenClockTrait` in your test to freeze the clock a the begining of your tests and manipulate time.

```
use Baldinof\Clock\Testing\FrozenClockTrait;
use Baldinof\Clock\Clock;
use PHPUnit\Framework\TestCase;

final class UserTest extends TestCase
{
    use FrozenClockTrait;

    public function test_it_is_initialized_with_current_time()
    {
        $user = new User("John");

        $this->assertEquals(Clock::now(), $user->createdAt());
    }
}
```

You can allso explicitly manipulate the clock

```
use Baldinof\Clock\Testing\FrozenClockTrait;
use Baldinof\Clock\Clock;
use PHPUnit\Framework\TestCase;

final class UserTest extends TestCase
{
    use FrozenClockTrait;

    public function my_function()
    {
        // Set the clock at a specified time.
        $this->freezeClock(new \DateTimeImmutable('2000-01-01'));

        // Add an hour to the clock.
        $this->modifyClock('+1 h');

        // Reset the clock.
        $this->restoreSystemClock();
    }
}
```

Timezones
---------

[](#timezones)

Out of the box `Clock::now()` does not pass the timezone argument to the `DateTimeImmutable` constructor, so the **php default timezone is used.**

You can change the default timezone in your `php.ini`.

Otherwise, call `date_default_timezone_set()` or call `Clock::set()` in your application bootstrap code:

```
