Search by

radynsade / database-clock

radynsade

PSR-20 clock implementation backed by databases server time.

Package info

github.com/radynsade/database-clock

pkg:composer/radynsade/database-clock

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v2.0.0 2026-08-17 18:24 UTC

This package is auto-updated.

Last update: 2026-08-17 18:29:33 UTC


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