radynsade / database-clock
PSR-20 clock implementation backed by databases server time.
v2.0.0
2026-08-17 18:24 UTC
Requires
- php: ^8.4.1
- psr/clock: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^13.3
Suggests
- ext-mysqli: Required by MySql\MysqliClock.
- ext-pdo: Required by any PdoClock.
- ext-pdo_mysql: Required by MySql\PdoClock.
- ext-pdo_pgsql: Required by PostgreSql\PdoClock.
- ext-pgsql: Required by PostgreSql\PgsqlClock.
Provides
Conflicts
None
Replaces
None
README
A PSR-20 clock implementation that reads the current time from a database server and returns it as an immutable UTC value.
The package supports MySQL, MariaDB, and PostgreSQL through PDO and native database extensions.
Requirements
- PHP 8.4.1 or later (below PHP 9.0)
- A supported database server
- The extension required by the selected adapter:
| Adapter | Required extensions |
|---|---|
DatabaseClock\MySql\PdoClock |
ext-pdo, ext-pdo_mysql |
DatabaseClock\MySql\MysqliClock |
ext-mysqli |
DatabaseClock\PostgreSql\PdoClock |
ext-pdo, ext-pdo_pgsql |
DatabaseClock\PostgreSql\PgsqlClock |
ext-pgsql |
Installation
composer require radynsade/database-clock
Usage
All adapters implement Psr\Clock\ClockInterface and return a DateTimeImmutable instance in UTC.
With PDO
use DatabaseClock\PostgreSql\PdoClock; use PDO; $connection = new PDO( 'pgsql:host=localhost;dbname=app', 'postgres', 'secret', ); $clock = new PdoClock($connection); $currentTime = $clock->now();
MySQL with MySQLi
use DatabaseClock\MySql\MysqliClock; use mysqli; $connection = new mysqli( 'localhost', 'root', 'secret', 'app', ); $clock = new MysqliClock($connection); $currentTime = $clock->now();
PostgreSQL with ext-pgsql
use DatabaseClock\PostgreSql\PgsqlClock; use RuntimeException; $connection = pg_connect('host=localhost dbname=app user=postgres password=secret'); if ($connection === false) { throw new RuntimeException('Failed to connect to PostgreSQL.'); } $clock = new PgsqlClock($connection); $currentTime = $clock->now();
Testing
composer test
To include the PDO and Pgsql integration tests for PostgreSQL, provide a connection URL:
POSTGRES_CLOCK_TEST_CONNECTION='postgresql://postgres:secret@127.0.0.1:5432/test' composer test
To include the PDO and MySQLi integration tests for MySQL or MariaDB, provide a connection URL:
MYSQL_CLOCK_TEST_CONNECTION='mysql://root:secret@127.0.0.1:3306/test' composer test
License
MIT