PHPackages                             stratadox/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. stratadox/clock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stratadox/clock
===============

v0.2(5y ago)014.6k[2 PRs](https://github.com/Stratadox/Clock/pulls)MITPHPPHP &gt;=7.2

Since Aug 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Stratadox/Clock)[ Packagist](https://packagist.org/packages/stratadox/clock)[ RSS](/packages/stratadox-clock/feed)WikiDiscussions master Synced 2w ago

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

Clock
=====

[](#clock)

Factory for creating datetime objects.

[![Build Status](https://camo.githubusercontent.com/a7247b31d6c2cefa7e381a32c5571ae2381e9eac416f6b3253135b9152796a3f/68747470733a2f2f7472617669732d63692e6f72672f537472617461646f782f436c6f636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Stratadox/Clock)[![Coverage Status](https://camo.githubusercontent.com/d9e82f3a7e16083d8952d62c5fc12269ca3e37ea4ba39497423e63a7371d2e60/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f537472617461646f782f436c6f636b2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Stratadox/Clock?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7c1a335449bbd11ee3f2273ffe30a902392617e329ed6b1e11f7ef15647634bf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537472617461646f782f436c6f636b2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Stratadox/Clock/?branch=master)

What
----

[](#what)

It's really as simple as the name suggests: this is a clock, used to indicate what time it is.

Since it produces [DateTime](https://www.php.net/datetimeinterface) objects, this clock is somewhat special in the sense that it can also read the date.

[![Just a clock, really.](https://camo.githubusercontent.com/2184ab0f1ec4db121cb6be316f0bdbc595694139d7925fc744de8313a554bc27/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f332f33612f42616c6c6f6f6e5f627261636b65745f636c6f636b5f4d45545f44502d31333132322d3034332e6a70672f35303070782d42616c6c6f6f6e5f627261636b65745f636c6f636b5f4d45545f44502d31333132322d3034332e6a7067)](https://camo.githubusercontent.com/2184ab0f1ec4db121cb6be316f0bdbc595694139d7925fc744de8313a554bc27/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f332f33612f42616c6c6f6f6e5f627261636b65745f636c6f636b5f4d45545f44502d31333132322d3034332e6a70672f35303070782d42616c6c6f6f6e5f627261636b65745f636c6f636b5f4d45545f44502d31333132322d3034332e6a7067)

Why
---

[](#why)

- As soon as you use "unadulterated" datetime objects in your code, any test you've written for it immediately risks being flaky, because if there's a tiny bit of time between `new DateTime` and your assertion, the test fails.
- Instantiating a `new DateTime` or `new DateTimeImmutable` in client code, is a static invocation. This introduces coupling and reduces testability. This obviously goes double for `date_create()` and the like.
- It's a lot more natural to get the time from a clock than to instantiate a new instant each time you want to know how late it is.

Installing
----------

[](#installing)

Install with `composer require stratadox/clock`

Examples
--------

[](#examples)

### Clock example

[](#clock-example)

In a service that needs to know the time:

```
