PHPackages                             slope-it/clock-mock - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. slope-it/clock-mock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

slope-it/clock-mock
===================

A library for mocking current date and time in tests.

0.5.0(1y ago)751.8M↓14.6%1810MITPHPPHP &gt;=8.0.0

Since May 23Pushed 1y ago6 watchersCompare

[ Source](https://github.com/slope-it/clock-mock)[ Packagist](https://packagist.org/packages/slope-it/clock-mock)[ RSS](/packages/slope-it-clock-mock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (17)Used By (10)

ClockMock
=========

[](#clockmock)

[Slope s.r.l.](https://www.slope.it)

[![Latest Stable Version](https://camo.githubusercontent.com/5b7f50e6d46a32524655c78cb3d192058cfdb5daa9967cad0280420d1b8d3b26/68747470733a2f2f706f7365722e707567782e6f72672f736c6f70652d69742f636c6f636b2d6d6f636b2f762f737461626c65)](https://packagist.org/packages/slope-it/clock-mock)[![Total Downloads](https://camo.githubusercontent.com/3753da55a8424977d0fa82a14b12e36607d8e1d83ec433406fc31f1868442303/68747470733a2f2f706f7365722e707567782e6f72672f736c6f70652d69742f636c6f636b2d6d6f636b2f646f776e6c6f616473)](https://packagist.org/packages/slope-it/clock-mock)[![License](https://camo.githubusercontent.com/cd96ba72c9baf2d9a2cd6a57914e8cd4315ab6a69b584b5a7904a96dd0fd2054/68747470733a2f2f706f7365722e707567782e6f72672f736c6f70652d69742f636c6f636b2d6d6f636b2f6c6963656e7365)](https://packagist.org/packages/slope-it/clock-mock)

ClockMock provides a way for mocking the current timestamp used by PHP for \\DateTime(Immutable) objects and date/time related functions. It requires the [uopz extension](https://github.com/krakjoe/uopz) (version &gt;= 6.1.1).

**This library is meant for development and testing only. It does not aim to propose a clock service to be used in production code, as we believe that you shouldn't need to do that when your only purpose is to mock the current time in testing code.**

Why we built it
---------------

[](#why-we-built-it)

Here is an article that explains in depth how and why this library was built: [link to the article](https://dev.to/andreasprega/clockmock-a-convenient-way-to-mock-date-and-time-in-php-5032).

TL;DR

- We were looking for a way to mock the native php date and time functions and classes without having to change our production code for it, and without having ot use any 3rd party library for handling dates/clocks.
- For this purpose, we were previously using the `php-timecop` extension but that never implemented support for PHP 7.4 onward.

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

[](#installation)

You can install the library using Composer. Run the following command to install the latest version from Packagist:

```
composer require --dev slope-it/clock-mock
```

Note that, as this is not a tool intended for production, it should be required only for development (`--dev` flag).

Mocked functions/methods
------------------------

[](#mocked-functionsmethods)

- date()
- date\_create()
- date\_create\_from\_format()
- date\_create\_immutable()
- date\_create\_immutable\_from\_format()
- getdate()
- gettimeofday()
- gmdate()
- gmmktime()
- gmstrftime() (DEPRECATED starting from PHP 8.1)
- idate()
- localtime()
- microtime()
- mktime()
- strftime() (DEPRECATED starting from PHP 8.1)
- strtotime()
- time()
- unixtojd()
- DateTime::\_\_construct
- DateTime::createFromFormat
- DateTimeImmutable::\_\_construct
- DateTimeImmutable::createFromFormat
- $\_SERVER\['REQUEST\_TIME'\]
- $\_SERVER\['REQUEST\_TIME\_FLOAT'\]

Usage
-----

[](#usage)

### 1. Stateful API

[](#1-stateful-api)

You can call `ClockMock::freeze` with a \\DateTime or \\DateTimeImmutable. Any code executed after it will use that specific date and time as the current timestamp. Call `ClockMock::reset` when done to restore real, current time.

Example:

```
