igor-popravka/ip-time-clock

PSR-20 Clock implementation based on server's geolocated IP timezone

v1.1.0 2025-05-20 20:02 UTC

This package is auto-updated.

Last update: 2025-05-20 20:03:33 UTC


README

Latest Version PHP Version License

A Composer package implementing \Psr\Clock\ClockInterface that returns the accurate current server time based on its geolocation (determined by IP address).

Purpose

This package provides the server's local time considering its timezone, which is automatically resolved via an external time API based on the server’s IP address. Useful when server time is set to UTC but localized time is needed.

Installation

composer require igor-popravka/ip-time-clock

Usage

use IpTimeClock\Clock;
use IpTimeClock\Adapter\TimeIpIoAdapter;
use GuzzleHttp\Client;

// Initialize HTTP client (Guzzle)
$client = new Client();

// Optionally specify an IP address, or null to use server's IP
$ip = null;

// Create adapter
$adapter = new TimeIpIoAdapter($client);

// Create Clock instance
$clock = new Clock($adapter);

// Get current localized time
$currentTime = $clock->now();

echo $currentTime->format('Y-m-d H:i:sP');

Configuration

  • By default, the package uses WorldTimeAPI to determine the timezone from the server's IP address.

  • You can specify a different IP address in the adapter constructor to get the time for a different location.

  • To use another time API provider, implement your own adapter by implementing the TimeApiAdapterInterface.

Testing

The package includes unit tests that mock the time API adapter to verify the correct behavior of the Clock class.

To run tests:

vendor/bin/phpunit