PHPackages                             comsolit/clock-bundle - 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. comsolit/clock-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

comsolit/clock-bundle
=====================

Clock Service to represent a request-constant, system-wide instance in time.

1.0.0(8y ago)04631[4 issues](https://github.com/comsolit/ComsolitClockBundle/issues)MITPHPPHP &gt;=5.5.0

Since Mar 19Pushed 3y ago8 watchersCompare

[ Source](https://github.com/comsolit/ComsolitClockBundle)[ Packagist](https://packagist.org/packages/comsolit/clock-bundle)[ RSS](/packages/comsolit-clock-bundle/feed)WikiDiscussions master Synced today

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

**THIS REPO IS UNMAINTAINED / A NEW MAINTAINER IS NEEDED**
==========================================================

[](#this-repo-is-unmaintained--a-new-maintainer-is-needed)

Clock Bundle for Symfony
========================

[](#clock-bundle-for-symfony)

[![Build Status](https://camo.githubusercontent.com/74fa3ea29bd395f17c202e2f7acfd71847d05a81b74e62a44474fb009bd57a2e/68747470733a2f2f7472617669732d63692e6f72672f636f6d736f6c69742f436f6d736f6c6974436c6f636b42756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/comsolit/ComsolitClockBundle)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7effc8d4f4e61f8732433bae0ca7abdcff9560d1bcf0df95a0dbe070a3b9fb98/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f6d736f6c69742f436f6d736f6c6974436c6f636b42756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/comsolit/ComsolitClockBundle/?branch=master)

Unit-Testing anything related to time is hard if the code under test directly accesses the system time. Therefor this bundle provides a clock service wrapping a DateTime instance that represents "now".

Another nice property is, that "now" remains the same during one request.

Usage Examples
--------------

[](#usage-examples)

The clock also contains some helper functions that help to create easily understandable code related to time.

### Test, whether a user may try to login again

[](#test-whether-a-user-may-try-to-login-again)

After some failures to login the user from a given IP address may not login for some minutes:

```
$clock->hasElapsed($blockingPeriod, $lastFailedLogin->getDateTime())
```

Without the helper methods this would look like:

```
((int)$lastFailedLogin->getDateTime()->format('U')) + $blockingPeriod >= $clock->getTimestamp()
```

### Test, whether a token is still valid

[](#test-whether-a-token-is-still-valid)

```
$clock->isExpired($tokenExpirationTime)
```

### Save a file with time as part of the filename

[](#save-a-file-with-time-as-part-of-the-filename)

```
$fileName = $clock->getFileName() . '_datadump';
// e.g. $fileName === '2015-03-25_23-12-59_datadump'
```

Helper methods in class Clock
-----------------------------

[](#helper-methods-in-class-clock)

```
class Clock
{
    // SNIPP some stuff

    /**
     * Time representation usable as (part of) a file name, e.g.: 2014-05-23_13-45-23
     * @return string
     */
    public function getFileName()
    {
        return $this->now->format('Y-m-d_H-i-s');
    }

    /**
     * @return \DateTime
     */
    public function getDateTime()
    {
        return $this->now;
    }

    /**
     * @return int
     */
    public function getSeconds()
    {
        return (int)$this->now->format('U');
    }

    /**
     * @return int
     */
    public function getSecondsSince(\DateTimeInterface $past)
    {
        return $this->getSeconds() - (int)$past->format('U');
    }

    /**
     * @return int
     */
    public function getSecondsUntil(\DateTimeInterface $future)
    {
        return - $this->getSecondsSince($future);
    }

    /**
     * @return bool
     * @param int $seconds
     */
    public function hasElapsed($seconds, \DateTimeInterface $since)
    {
        return $this->getSecondsSince($since) > $seconds;
    }

    /**
     * @return bool
     */
    public function isExpired(\DateTimeInterface $expiryDate)
    {
        return (int)$this->now->format('U') > (int)$expiryDate->format('U');
    }

    /**
     * Create new DateTime object with the timezone of this clock
     *
     * @param String $time
     */
    public function createDateTime($time)
    {
        return (new \DateTime($time, $this->now->getTimezone()))->setTimezone($this->now->getTimezone());
    }

    /**
     * Returns an instance of DateTimeImmutable for the given implementation of DateTimeInterface.
     *
     * This method should rather exist in the DateTimeImmutable class but Derick Rethans doesn't
     * think so.
     *
     * @param \DateTimeInterface $datetime
     * @return \DateTimeImmutable
     */
    public static function createDateTimeImmutable(\DateTimeInterface $datetime)
    {
        if ($datetime instanceof \DateTimeImmutable)
        {
            return $datetime;
        }

        return \DateTimeImmutable::createFromMutable($datetime);
    }
}
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

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

Total

4

Last Release

3055d ago

Major Versions

0.3 → 1.0.02018-02-20

PHP version history (2 changes)0.1PHP &gt;=5.4.0

0.3PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/330bd5b6615b47b79b7a91898ac6b588b3c055e999c5fa10ee6037a9d801c9c1?d=identicon)[comsolit](/maintainers/comsolit)

---

Top Contributors

[![clecherbauer](https://avatars.githubusercontent.com/u/985388?v=4)](https://github.com/clecherbauer "clecherbauer (1 commits)")

---

Tags

looking-for-maintainerunmaintainedclock bundle

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/comsolit-clock-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/comsolit-clock-bundle/health.svg)](https://phpackages.com/packages/comsolit-clock-bundle)
```

###  Alternatives

[nystudio107/craft-richvariables

Allows you to easily use Craft Globals as variables in Rich Text fields

2523.8k2](/packages/nystudio107-craft-richvariables)

PHPackages © 2026

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