PHP code example of hexogen / timesync

1. Go to this page and download the library: Download hexogen/timesync library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hexogen / timesync example snippets


use Psr\Http\Client\ClientExceptionInterface;

try {
    $clock = $syncService->getCurrentTime();
} catch (\InvalidArgumentException $e) {
    // Invalid IPv4 address format
} catch (\RuntimeException $e) {
    // API error, malformed response, or detector failure
} catch (ClientExceptionInterface $e) {
    // HTTP client error
}

use Hexogen\Timesync\GeolocationServiceException;
use Hexogen\Timesync\InvalidIpAddressException;
use Hexogen\Timesync\ServerIpDetectionException;
use Psr\Http\Client\ClientExceptionInterface;

try {
    $clock = $syncService->getCurrentTime();
} catch (InvalidIpAddressException $e) {
    // Invalid IPv4 address format
} catch (GeolocationServiceException $e) {
    // ipgeolocation.io returned an error or malformed payload
} catch (ServerIpDetectionException $e) {
    // The current server IP could not be determined
} catch (ClientExceptionInterface $e) {
    // HTTP client transport error
}



use GuzzleHttp\Client;
use Hexogen\Timesync\IPGeolocationClient;
use Hexogen\Timesync\IpifyIPDetector;
use Hexogen\Timesync\SyncService;

$httpClient = new Client();
$ipDetector = new IpifyIPDetector($httpClient);
$syncClient = new IPGeolocationClient(
    apiKey: 'your-ipgeolocation-api-key',
    httpClient: $httpClient,
);
$syncService = new SyncService($syncClient, $ipDetector);

$clock = $syncService->getCurrentTime();
echo $clock->now()->format('Y-m-d H:i:s.u');

use Hexogen\Timesync\Clock;

$referenceTime = microtime(true) + 5.0;
$clock = new Clock($referenceTime, 'Europe/Kyiv');

$now = $clock->now();

use GuzzleHttp\Client;
use Hexogen\Timesync\IPGeolocationClient;

$httpClient = new Client();
$client = new IPGeolocationClient('your-api-key', $httpClient);

$clock = $client->getCurrentTime('8.8.8.8');
echo $clock->now()->getTimezone()->getName();

use GuzzleHttp\Client;
use Hexogen\Timesync\IPGeolocationClient;
use Hexogen\Timesync\IpifyIPDetector;
use Hexogen\Timesync\SyncService;

$httpClient = new Client();
$ipDetector = new IpifyIPDetector($httpClient);
$syncClient = new IPGeolocationClient('your-api-key', $httpClient);
$syncService = new SyncService($syncClient, $ipDetector);

$clock = $syncService->getCurrentTime();

$clock = $syncService->getCurrentTime('37.17.245.123');

use Hexogen\Timesync\ServerIPDetectorInterface;

class CustomIPDetector implements ServerIPDetectorInterface
{
    public function getCurrentServerIP(): string
    {
        return $this->someService->fetchServerIP();
    }
}

public function __construct(
    float $realTimestamp,
    string $timeZone = 'UTC'
)

public function now(): DateTimeImmutable

public function __construct(
    string $apiKey,
    ClientInterface $httpClient
)

public function getCurrentTime(string $ip): ClockInterface

public function __construct(
    SyncClientInterface $syncClient,
    ServerIPDetectorInterface $serverIPDetector
)

public function getCurrentTime(?string $ip = null): ClockInterface

public function __construct(ClientInterface $httpClient)

public function getCurrentServerIP(): string

$clock = new Clock(microtime(true), 'America/New_York');

use Hexogen\Timesync\GeolocationServiceException;
use Hexogen\Timesync\InvalidIpAddressException;
use Hexogen\Timesync\ServerIpDetectionException;
use Hexogen\Timesync\TimesyncException;
use Psr\Http\Client\ClientExceptionInterface;

try {
    $clock = $syncService->getCurrentTime();
} catch (InvalidIpAddressException $e) {
    // Invalid IPv4 address format supplied to the library
    echo $e->getMessage();
} catch (GeolocationServiceException $e) {
    // ipgeolocation.io returned an error or unexpected payload
    echo $e->getMessage();
} catch (ServerIpDetectionException $e) {
    // The current server IP could not be determined
    echo $e->getMessage();
} catch (ClientExceptionInterface $e) {
    // HTTP client transport error
    echo $e->getMessage();
} catch (TimesyncException $e) {
    // Optional single catch for any other library-specific exception type
    echo $e->getMessage();
}

// In a service provider
$this->app->singleton(ClockInterface::class, function () {
    $httpClient = new \GuzzleHttp\Client();
    $ipDetector = new IpifyIPDetector($httpClient);
    $syncClient = new IPGeolocationClient(
        config('services.ipgeolocation.key'),
        $httpClient,
    );
    $syncService = new SyncService($syncClient, $ipDetector);

    return $syncService->getCurrentTime();
});
bash
composer fix