PHPackages                             alex-no/server-time-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. alex-no/server-time-clock

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

alex-no/server-time-clock
=========================

PSR-20 clock implementation that returns the current server time based on its local timezone.

v1.0.7(12mo ago)014MITPHPPHP ^8.1

Since May 7Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/alex-no/server-time-clock)[ Packagist](https://packagist.org/packages/alex-no/server-time-clock)[ Docs](https://github.com/alex-no/server-time-clock)[ RSS](/packages/alex-no-server-time-clock/feed)WikiDiscussions master Synced 1mo ago

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

Server Time Clock
=================

[](#server-time-clock)

[![Latest Stable Version](https://camo.githubusercontent.com/5d3249b88a801b2ae640222344fd1d719da8a188cdc598b0c092ad9bda54b25e/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6e6f2f7365727665722d74696d652d636c6f636b2f762f737461626c65)](https://packagist.org/packages/alex-no/server-time-clock)[![Total Downloads](https://camo.githubusercontent.com/0b1b3050e65b0e6820cefa154323083d1a109d8a6021cabb990bdaa4421bdd6f/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6e6f2f7365727665722d74696d652d636c6f636b2f646f776e6c6f616473)](https://packagist.org/packages/alex-no/server-time-clock)[![License](https://camo.githubusercontent.com/5d7b5b85cee6cfe4e7793736d774afec9f7f4a9f1d6863298a91052fdd80b891/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6e6f2f7365727665722d74696d652d636c6f636b2f6c6963656e7365)](https://packagist.org/packages/alex-no/server-time-clock)[![PHPUnit](https://github.com/alex-no/server-time-clock/actions/workflows/phpunit.yml/badge.svg)](https://github.com/alex-no/server-time-clock/actions)

> PSR-20 clock implementation that returns the current server time based on its local timezone or online providers.

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

[](#installation)

Install via [Composer](https://getcomposer.org):

```
composer require alex-no/server-time-clock
```

Usage
-----

[](#usage)

```
use ServerTimeClock\ServerClock;

$clock = new ServerClock([
    'client' => 'WorldTimeApi', // or 'IpGeoLocation', 'TimeApiIo'
    'credentials' => [
        'IpGeoLocation' => 'your-api-key', // optional, depending on the provider
        'WorldTimeApi' => 'your-api-key2',
    ],
    'enable_cache' => true,
    'cache_ttl' => 300, // seconds
    'useMock' => false, // Mosk is usually used for testing.
]);

$now = $clock->now(); // instance of DateTimeImmutable
echo $now->format(DATE_ATOM);
```

Configuration Options
---------------------

[](#configuration-options)

KeyTypeDescription`client`stringPreferred time provider (`WorldTimeApi`, `IpGeoLocation`, etc.)`credentials`arrayAPI keys for time providers (optional)`enable_cache`boolEnable APCu-based caching (default: `false`)`cache_ttl`intCache duration in seconds`useMock`boolEnable/Disable Mock for tests (default: `false`)Providers
---------

[](#providers)

🌐 WorldTimeApi – no key required

🌐 IpGeoLocation – requires free API key from ipgeolocation.io

🌐 TimeApiIo – no key required

Laravel Integration
-------------------

[](#laravel-integration)

You can integrate this package into Laravel with minimal setup.

### Configuration (Optional)

[](#configuration-optional)

To publish the config file:

```
php artisan vendor:publish --tag=server-clock-config
```

This will create config/server-clock.php:

```
return [
    'client' => 'WorldTimeApi', // Preferred time provider
    'credentials' => [
        'IpGeoLocation' => 'your-api-key',
        'WorldTimeApi' => 'your-api-key2',
    ],
    'enable_cache' => true,
    'cache_ttl' => 300,
];
```

### Usage in Laravel

[](#usage-in-laravel)

```
use ServerTimeClock\ServerClock;

$clock = app(ServerClock::class);
echo $clock->now()->format('c');
```

Yii2 Integration
----------------

[](#yii2-integration)

### Configuration

[](#configuration)

You may use the ServerClock class directly or configure it as a Yii2 component:

```
'components' => [
    'serverClock' => [
        'class' => \ServerTimeClock\Yii\ServerClockComponent::class,
        'client' => 'WorldTimeApi',  // Preferred time provider
        'credentials' => [
            'IpGeoLocation' => 'your-api-key',
            'WorldTimeApi' => 'your-api-key2',
        ],
        'enableCache' => true,
        'cacheTtl' => 300,
    ],
],
```

### Usage

[](#usage-1)

Then use it:

```
Yii::$app->serverClock->now();
```

> **Note**: Yii2 support is optional and requires yiisoft/yii2 to be installed in your project.

Optional Dependencies
---------------------

[](#optional-dependencies)

This package is framework-agnostic. Laravel and Yii2 integrations are provided for convenience.

FrameworkPackageRequired?Laravel`laravel/framework`OptionalYii2`yiisoft/yii2`OptionalAPCu Cache Requirements
-----------------------

[](#apcu-cache-requirements)

If you enable caching (enable\_cache =&gt; true), make sure the [APCu extension](https://www.php.net/manual/en/book.apcu.php) is installed and enabled in your PHP configuration. This typically means:

- The apcu extension must be installed (e.g., via pecl install apcu or appropriate package manager).
- For CLI usage (e.g., when running tests), ensure apc.enable\_cli=1 is set in your php.ini.

You can verify if APCu is available by running:

```
php -i | grep apcu
```

Testing
-------

[](#testing)

Run PHPUnit tests:

```
vendor/bin/phpunit
```

License
-------

[](#license)

MIT License

Made with ❤️ by Oleksandr Nosov

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance50

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Total

7

Last Release

362d ago

PHP version history (2 changes)v1.0.0PHP ^8.3

v1.0.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/9467184a6504a716bb639f68f9a23ef18735335fe3d625ae8839b726ae4c89cb?d=identicon)[alex-no](/maintainers/alex-no)

---

Top Contributors

[![alex-no](https://avatars.githubusercontent.com/u/6074502?v=4)](https://github.com/alex-no "alex-no (24 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alex-no-server-time-clock/health.svg)

```
[![Health](https://phpackages.com/badges/alex-no-server-time-clock/health.svg)](https://phpackages.com/packages/alex-no-server-time-clock)
```

###  Alternatives

[lcobucci/clock

Yet another clock abstraction

790190.9M114](/packages/lcobucci-clock)[symfony/clock

Decouples applications from the system clock

430168.9M205](/packages/symfony-clock)[eventsauce/eventsauce

A pragmatic event sourcing library for PHP with a focus on developer experience.

8632.1M47](/packages/eventsauce-eventsauce)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.4k423.9k30](/packages/mcp-sdk)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

374468.4k51](/packages/flow-php-etl)

PHPackages © 2026

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