PHP code example of tarasterletskij / true-service-time

1. Go to this page and download the library: Download tarasterletskij/true-service-time 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/ */

    

tarasterletskij / true-service-time example snippets


use TrueServiceTime\Application\Factory\TimeServiceFactory;

try {
    $service = TimeServiceFactory::create();
    $clock = $service->getServerTime();

    echo $clock->now()->format('Y-m-d H:i:s');
    echo $clock->now()->getTimezone()->getName();
} catch (\Exception $e) {
    // Handle error
}

$service = TimeServiceFactory::create();
$clock = $service->getServerTime('8.8.8.8');

use GuzzleHttp\Client;
use TrueServiceTime\Application\Factory\TimeServiceFactory;

$httpClient = new Client([
    'timeout' => 10.0,
    'verify' => true,
]);

$service = TimeServiceFactory::create($httpClient);
$clock = $service->getServerTime();

use GuzzleHttp\Client;
use TrueServiceTime\Application\Service\TimeService;
use TrueServiceTime\Infrastructure\TimeProvider\TimeApiIoProvider;
use TrueServiceTime\Infrastructure\IpDetector\IpApiDetector;

$httpClient = new Client(['timeout' => 5.0]);

$timeProvider = new TimeApiIoProvider($httpClient);
$ipDetector = new IpApiDetector($httpClient);
$service = new TimeService($timeProvider, $ipDetector);
$clock = $service->getServerTime();

use TrueServiceTime\Domain\Exception\TimeProviderException;
use TrueServiceTime\Domain\Exception\IpDetectionException;

$service = TimeServiceFactory::create();

try {
    $clock = $service->getServerTime();
} catch (TimeProviderException $e) {
    // Handle time provider API failure
} catch (IpDetectionException $e) {
    // Handle IP detection failure
} catch (\InvalidArgumentException $e) {
    // Handle invalid IP address
}

src/
├── Domain/
│   ├── Entity/ServerTime.php
│   ├── ValueObject/IpAddress.php
│   ├── ValueObject/TimeZone.php
│   ├── Interface/TimeProviderInterface.php
│   ├── Interface/IpDetectorInterface.php
│   └── Exception/
├── Application/
│   ├── Service/TimeService.php
│   ├── Clock/ServerTimeClock.php
│   └── Factory/TimeServiceFactory.php
└── Infrastructure/
    ├── TimeProvider/
    │   ├── TimeProviderInterface.php
    │   └── TimeApiIoProvider.php
    └── IpDetector/
        ├── IpDetectorInterface.php
        └── IpApiDetector.php