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

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

carpiftw/clock-mock
===================

A library for mocking current date and time in tests.

0.3.0(4y ago)07.5kMITPHPPHP &gt;=7.4.0

Since May 23Pushed 4y agoCompare

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

READMEChangelogDependencies (3)Versions (5)Used By (0)

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)

- 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. The problem is that said extension never implemented support for PHP 7.4 onward. That extension currently does not even build for PHP 8.0.

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\_immutable()
- getdate()
- gmdate()
- idate()
- localtime()
- microtime()
- strtotime()
- time()
- DateTime::\_\_construct
- DateTimeImmutable::\_\_construct

Functions/methods with missing mocks (HELP NEEDED!)
---------------------------------------------------

[](#functionsmethods-with-missing-mocks-help-needed)

- date\_create\_from\_format()
- date\_create\_immutable\_from\_format()
- gettimeofday()
- gmmktime()
- gmstrftime()
- mktime()
- strftime()
- unixtojd()
- DateTime::createFromFormat
- DateTimeImmutable::createFromFormat
- $\_SERVER\['REQUEST\_TIME'\]

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:

```
