dmkravchuk/ip-clock
最新稳定版本:v1.0.0
Composer 安装命令:
composer require dmkravchuk/ip-clock
包简介
Resolves accurate server time and timezone via external IP lookup
README 文档
README
A PSR-20 compliant Composer package that returns the correct server time and timezone based on the server's external IP address.
Requirements
- PHP ^8.2
- Composer
Installation
Via Packagist
composer require dmkravchuk/ip-clock
Via VCS (without Packagist)
Add to your composer.json:
"repositories": [ { "type": "vcs", "url": "https://github.com/dmkravchuk/ip-clock" } ]
Then run:
composer require dmkravchuk/ip-clock
Configuration
One of the time providers — ipgeolocation.io — requires a free API key.
- Create a free account at https://ipgeolocation.io
- Check the Dashboard the API KEY section
- Copy your API key
- Set it as an environment variable in
.envfile:
IPGEOLOCATION_API_KEY=your_api_key_here
The package works without this key — ipgeolocation.io will simply be skipped if the key is not set.
Usage
Basic usage (auto-detect server IP)
use DmKravchuk\IpClock\ClockFactory; $clock = ClockFactory::create(); $now = $clock->now(); echo $now->format('Y-m-d H:i:s'); // 2026-03-10 14:00:00 echo $now->getTimezone()->getName(); // Europe/Kyiv
With explicit IP address
$clock = ClockFactory::create(ip: '8.8.8.8'); $now = $clock->now(); echo $now->getTimezone()->getName(); // America/Los_Angeles
With custom PSR-3 logger
use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new Logger('ip-clock'); $logger->pushHandler(new StreamHandler('php://stdout')); $clock = ClockFactory::create(logger: $logger);
PSR-20 compatibility
use Psr\Clock\ClockInterface; function doSomething(ClockInterface $clock): void { $now = $clock->now(); } doSomething(ClockFactory::create());
How it works
- Resolves the server's external IP via ipify.org
- Determines the timezone using a chain of time providers:
- Primary: timeapi.io — free, no API key required
- Fallback #1: worldtimeapi.org — free, no API key required
- Fallback #2: ipgeolocation.io — free API key required
- If all providers fail — returns current time in UTC
Running tests
composer test
License
MIT
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-10