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

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

maba/clock
==========

0.1.1(10y ago)2136MITPHPPHP &gt;=5.3.2

Since Jul 23Pushed 10y ago1 watchersCompare

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

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

Clock PHP library
=================

[](#clock-php-library)

What's that?
------------

[](#whats-that)

1. `ClockInterface` with single method `getCurrentDateTime` (`@returns \DateTime`)

    Why not just `new \DateTime()`? Because sometimes you need to adjust the time. Depending on global context (read `new \DateTime()`) is just wrong, as you cannot change the behaviour by dependency injection, and thus configuration.

    Few examples: 1) unit tests - you need to mock the clock 2) you need to alter the timezone and you don't want to change php.ini settings (see `TimeZonedClock`)
2. Classes in `Condition` namespace for checking and prioritizing time conditions.

### Clock

[](#clock)

`RealTimeClock` - gives the real, unaltered `\DateTime`

`TimeZonedClock` - gets time from another `ClockInterface` (usually `RealTimeClock`) and sets pre-configured timezone.

### Condition

[](#condition)

`TimeCondition` - POJO/Value object, which contains available time restrictions:

1. From time, in seconds, inclusive
2. Until time, in seconds, exclusive. Can overlap 00:00 with from time
3. Weekday. `0` (Sunday) to `6` (Saturday)
4. Day of month (`1` to `31`)
5. Month (`1` to `12`). Can be specified only with day of month
6. Year (for example `2015`). Can be specified only with month and day of month

You can implement `TimeConditionInterface` and use this library with your own objects, for example any Doctrine Entity.

`ConditionPriorityResolver` - gives priority for any `TimeConditionInterface`. Larger priority means that condition is more concrete, thus should "win" if choosing between several.

`TimeConditionChecker` - returns whether given `TimeConditionInterface` matches given `\DateTime`

`CurrentTimeConditionChecker` - returns whether given `TimeConditionInterface` matches current time

`ConditionalValueResolver` - resolves most concrete time condition matching current time and gives related value.

Example:

```
