PHPackages                             mawebdk/utc-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. [Testing &amp; Quality](/categories/testing)
4. /
5. mawebdk/utc-datetime

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

mawebdk/utc-datetime
====================

Representation of a UTC date and time with easy mocking of current date and time for testing.

2.0.1(8mo ago)09BSD-2-ClausePHPPHP ^8.3.6

Since Feb 23Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/mawebdk/utc-datetime)[ Packagist](https://packagist.org/packages/mawebdk/utc-datetime)[ RSS](/packages/mawebdk-utc-datetime/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

utc-datetime
============

[](#utc-datetime)

This package contains a representation of a UTC date and time with easy mocking of current date and time for testing.

Use UtcDateTime and UtcDateTimeImmutable to handle UTC date and times.

UtcDateTime and UtcDateTimeImmutable behaves the same, except:

- When calling modification methods on a UtcDateTime object, the object is modified itself.
- When calling modification methods on a UtcDateTimeImmutable object, a new object will be returned.

Usage
-----

[](#usage)

Create a new UtcDateTime object with the current UTC date and time.

Note: Method now() uses Clock to get current date and time to enable support for mocking of current date and time.

```
$UtcDateTime = UtcDateTime::now();

```

Create a new UtcDateTime object with a given Unix timestamp with optional milliseconds or microseconds.

```
$utcDateTime = UtcDateTime::createFromUnixTimestamp(unixTimestamp: $unixTimestamp);
$utcDateTime = UtcDateTime::createFromUnixMilliTimestamp(unixMilliTimestamp: $unixMilliTimestamp);
$utcDateTime = UtcDateTime::createFromUnixMicroTimestamp(unixMicroTimestamp: $unixMicroTimestamp);

```

Get Unix timestamp with optional milliseconds or microseconds from a UtcDateTime object.

```
$unixTimestamp      = $utcDateTime->getUnixTimestamp;
$unixMilliTimestamp = $utcDateTime->getUnixMilliTimestamp;
$unixMicroTimestamp = $utcDateTime->getUnixMicroTimestamp;

```

Get MySQL date and time with microseconds format from a UtcDateTime object.

```
$mysqlDateTime6 = $utcDateTime->formatMysqlDateTime6();

```

Mocking of current UTC date and time
------------------------------------

[](#mocking-of-current-utc-date-and-time)

Use ReflectionClass to set the Clock singleton to a mocked class implementing ClockInterface.

```
/**
 * @throws Exception
 * @throws UtcDateTimeException
 */
public function testNow_Mocking()
{
    $mockedDateTimeImmutable = DateTimeImmutable::createFromFormat(
        format: 'Y-m-d H:i:s.u',
        datetime: '1999-12-31 23:59:59.999999',
        timezone: new DateTimeZone(timezone: 'UTC')
    );

    $mockedClock = $this->createMock(type: ClockInterface::class);
    $mockedClock
        ->method(constraint: 'now')
        ->willReturn($mockedDateTimeImmutable);

    $reflectionClass = new ReflectionClass(objectOrClass: Clock::class);
    $reflectionClass->setStaticPropertyValue(name: 'singleton', value: $mockedClock);

    $this->assertSame(
        expected: $mockedDateTimeImmutable->format(format: 'Y-m-d H:i:s.u'),
        actual: UtcDateTimeImmutable::now()->formatMysqlDateTime6()
    );
}

```

Remember to reset singleton to ensure usage of correct date and time in subsequent tests.

```
protected function tearDown(): void
{
    // Reset singleton to ensure usage of correct date and time in subsequent tests.
    $reflectionClass = new ReflectionClass(objectOrClass: Clock::class);
    $reflectionClass->setStaticPropertyValue(name: 'singleton', value: null);
}

```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance60

Regular maintenance activity

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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 ~48 days

Total

5

Last Release

256d ago

Major Versions

1.2.0 → 2.0.02025-09-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/ff52225bf61e0534f8e176e62a068779155c4121dccee2223b9814828e6a5867?d=identicon)[mawebdk](/maintainers/mawebdk)

---

Top Contributors

[![mawebdk](https://avatars.githubusercontent.com/u/176606063?v=4)](https://github.com/mawebdk "mawebdk (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

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