PHPackages                             shopery/datetime - 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. shopery/datetime

ActiveLibrary

shopery/datetime
================

Datetime helper for easier testing

0.2(8y ago)17.9k2MITPHPPHP &gt;=5.5

Since Mar 21Pushed 8y ago4 watchersCompare

[ Source](https://github.com/shopery/datetime)[ Packagist](https://packagist.org/packages/shopery/datetime)[ RSS](/packages/shopery-datetime/feed)WikiDiscussions master Synced today

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

shopery/datetime
================

[](#shoperydatetime)

⚠ Note ⚠
--------

[](#-note-)

This library is not production ready yet, and can be subject to heavy changes.

Simple `DateTime` provider. Allows time freezing for testing purposes.

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

[](#installation)

You can install this library:

- Install via [composer](https://getcomposer.org): `composer require shopery/datetime`
- Use the [official Git repository](https://github.com/shopery/datetime): `git clone https://github.com/shopery/datetime`.

Usage
-----

[](#usage)

Replace every `new DateTime()` with a call to `Shopery\DateTime\DateTime::now()` for better testability.

To create relative datetimes, be it in the past or the future, use `Shopery\DateTime\DateTime::create($timeString)` passing a [GNU string modificator](http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html).

Freezing time
-------------

[](#freezing-time)

You can freeze time with the static method `DateTime::freeze()`. Pass any date as argument to set the current frozen time. This allows testing time-dependent scenarios. You should remember to call `DateTime::unfreeze` at the end of the tests to clear it.

Example
-------

[](#example)

```
use Shopery\DateTime\DateTime;
use DateTime as NativeDateTime;

class Coupon
{
    private $expiredTime;

    public function __construct(NativeDateTime $expiredTime)
    {
        $this->expiredTime = $expiredTime;
    }

    public function hasExpired()
    {
        return DateTime::now() > $this->expiredTime;
    }
}

class CouponTest
{
    public function test_has_expired()
    {
        $coupon = new Coupon(DateTime::create('yesterday'));

        $this->assertTrue($coupon->hasExpired());

        $frozenTime = DateTime::create('a month ago');
        DateTime::freeze($frozenTime);

        $this->assertFalse($coupon->hasExpired());

        DateTime::unfreeze();
    }
}
```

PHPUnit Listener
----------------

[](#phpunit-listener)

You can setup PHPUnit to freeze time while entering some of your tests. A PHPUnit listener is provided which adds a `freezeTime` annotation for your tests. Here are the changes you need to do to your `phpunit.xml`:

```

    ...

    ...

```

And now, you can add `@freezeTime` in the PHPDoc of each test.

```
class MyTest
{
    /**
     * @freezeTime
     */
    public function test_frozen_in_current_time()
    {
    }

    /**
     * @freezeTime 2015-01-31 08:30:00
     */
    public function test_frozen_in_a_given_date()
    {
    }

    /**
     * @freezeTime first monday of January last year
     */
    public function test_frozen_in_a_relative_date()
    {
    }

    public function test_this_is_not_frozen()
    {
    }
}
```

###  Health Score

29

—

LowBetter than 63% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~430 days

Total

2

Last Release

3222d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/608fb6577803c78b31cf7ade299388f44ffe2e13f4cc773c331944773cca6b97?d=identicon)[xphere](/maintainers/xphere)

![](https://www.gravatar.com/avatar/7a9d632acc1c07a48dcb8883e3a4f55adecbd6cb17a9ebcc4c59b2d158dc7aac?d=identicon)[rogergros](/maintainers/rogergros)

---

Top Contributors

[![xphere](https://avatars.githubusercontent.com/u/170968?v=4)](https://github.com/xphere "xphere (10 commits)")[![rogergros](https://avatars.githubusercontent.com/u/2053670?v=4)](https://github.com/rogergros "rogergros (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shopery-datetime/health.svg)

```
[![Health](https://phpackages.com/badges/shopery-datetime/health.svg)](https://phpackages.com/packages/shopery-datetime)
```

PHPackages © 2026

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