alex-no/server-time-clock
Composer 安装命令:
composer require alex-no/server-time-clock
包简介
PSR-20 clock implementation that returns the current server time based on its local timezone.
README 文档
README
PSR-20 clock implementation that returns the current server time based on its local timezone or online providers.
Installation
Install via Composer:
composer require alex-no/server-time-clock
Usage
use ServerTimeClock\ServerClock; $clock = new ServerClock([ 'client' => 'WorldTimeApi', // or 'IpGeoLocation', 'TimeApiIo' 'credentials' => [ 'IpGeoLocation' => 'your-api-key', // optional, depending on the provider 'WorldTimeApi' => 'your-api-key2', ], 'enable_cache' => true, 'cache_ttl' => 300, // seconds 'useMock' => false, // Mosk is usually used for testing. ]); $now = $clock->now(); // instance of DateTimeImmutable echo $now->format(DATE_ATOM);
Configuration Options
| Key | Type | Description |
|---|---|---|
client |
string | Preferred time provider (WorldTimeApi, IpGeoLocation, etc.) |
credentials |
array | API keys for time providers (optional) |
enable_cache |
bool | Enable APCu-based caching (default: false) |
cache_ttl |
int | Cache duration in seconds |
useMock |
bool | Enable/Disable Mock for tests (default: false) |
Providers
🌐 WorldTimeApi – no key required
🌐 IpGeoLocation – requires free API key from ipgeolocation.io
🌐 TimeApiIo – no key required
Laravel Integration
You can integrate this package into Laravel with minimal setup.
Configuration (Optional)
To publish the config file:
php artisan vendor:publish --tag=server-clock-config
This will create config/server-clock.php:
return [ 'client' => 'WorldTimeApi', // Preferred time provider 'credentials' => [ 'IpGeoLocation' => 'your-api-key', 'WorldTimeApi' => 'your-api-key2', ], 'enable_cache' => true, 'cache_ttl' => 300, ];
Usage in Laravel
use ServerTimeClock\ServerClock; $clock = app(ServerClock::class); echo $clock->now()->format('c');
Yii2 Integration
Configuration
You may use the ServerClock class directly or configure it as a Yii2 component:
'components' => [ 'serverClock' => [ 'class' => \ServerTimeClock\Yii\ServerClockComponent::class, 'client' => 'WorldTimeApi', // Preferred time provider 'credentials' => [ 'IpGeoLocation' => 'your-api-key', 'WorldTimeApi' => 'your-api-key2', ], 'enableCache' => true, 'cacheTtl' => 300, ], ],
Usage
Then use it:
Yii::$app->serverClock->now();
Note: Yii2 support is optional and requires yiisoft/yii2 to be installed in your project.
Optional Dependencies
This package is framework-agnostic. Laravel and Yii2 integrations are provided for convenience.
| Framework | Package | Required? |
|---|---|---|
| Laravel | laravel/framework |
Optional |
| Yii2 | yiisoft/yii2 |
Optional |
APCu Cache Requirements
If you enable caching (enable_cache => true), make sure the APCu extension is installed and enabled in your PHP configuration. This typically means:
- The apcu extension must be installed (e.g., via pecl install apcu or appropriate package manager).
- For CLI usage (e.g., when running tests), ensure apc.enable_cli=1 is set in your php.ini.
You can verify if APCu is available by running:
php -i | grep apcu
Testing
Run PHPUnit tests:
vendor/bin/phpunit
License
MIT License
Made with ❤️ by Oleksandr Nosov
alex-no/server-time-clock 适用场景与选型建议
alex-no/server-time-clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alex-no/server-time-clock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alex-no/server-time-clock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-07