cakephp/chronos
Composer 安装命令:
composer require cakephp/chronos
包简介
A simple API extension for DateTime.
README 文档
README
Chronos focuses on providing immutable date/datetime objects. Immutable objects help ensure that datetime objects aren't accidentally modified, keeping data more predictable.
Installation
Installing with composer:
$ composer require cakephp/chronos
For details on the (minimum/maximum) PHP version see version map.
Usage
<?php require 'vendor/autoload.php'; use Cake\Chronos\Chronos; printf("Now: %s", Chronos::now());
Differences with nesbot/carbon
Chronos was originally compatible with Carbon but has diverged and no longer extends the PHP DateTime and DateTimeImmutable classes.
Immutable Object Changes
Immutable objects have a number of advantages:
- Using immutable objects is always free of side-effects.
- Dates and times don't accidentally change underneath other parts of your code.
With those benefits in mind, there are a few things you need to keep in mind when modifying immutable objects:
// This will lose modifications $date = new Chronos('2015-10-21 16:29:00'); $date->modify('+2 hours'); // This will keep modifications $date = new Chronos('2015-10-21 16:29:00'); $date = $date->modify('+2 hours');
Calendar Dates
PHP only offers datetime objects as part of the native extensions. Chronos adds
a number of conveniences to the traditional DateTime object and introduces
a ChronosDate object. ChronosDate instances their time frozen to 00:00:00 and the timezone
set to the server default timezone. This makes them ideal when working with
calendar dates as the time components will always match.
use Cake\Chronos\ChronosDate; $today = new ChronosDate(); echo $today; // Outputs '2015-10-21' echo $today->modify('+3 hours'); // Outputs '2015-10-21'
Like instances of Chronos, ChronosDate objects are also immutable.
Time-only Values
When you need to work with just times (without dates), use ChronosTime:
use Cake\Chronos\ChronosTime; $time = new ChronosTime('14:30:00'); echo $time->format('g:i A'); // 2:30 PM // Create from components $time = ChronosTime::create(14, 30, 0); // Arithmetic $later = $time->addHours(2)->addMinutes(15);
ChronosTime is useful for recurring schedules, business hours, or any scenario
where the date is irrelevant.
Testing with Chronos
Chronos provides setTestNow() to freeze time during testing:
use Cake\Chronos\Chronos; // Freeze time for predictable tests Chronos::setTestNow('2024-01-15 10:00:00'); $now = Chronos::now(); // Always 2024-01-15 10:00:00 // Reset to real time Chronos::setTestNow(null);
PSR-20 Clock Interface
For dependency injection, use ClockFactory which implements PSR-20:
use Cake\Chronos\ClockFactory; $clock = new ClockFactory('UTC'); $now = $clock->now(); // Returns Chronos instance // In your service class OrderService { public function __construct(private ClockInterface $clock) {} public function createOrder(): Order { return new Order(createdAt: $this->clock->now()); } }
Documentation
A more descriptive documentation can be found at book.cakephp.org/chronos/3/.
API Documentation
API documentation can be found on api.cakephp.org/chronos.
cakephp/chronos 适用场景与选型建议
cakephp/chronos 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.88M 次下载、GitHub Stars 达 1.36k, 最近一次更新时间为 2015 年 09 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「date」 「datetime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cakephp/chronos 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cakephp/chronos 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cakephp/chronos 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Time provider, providing consistent current date, time, datetime and timezone across the request.
Adds the EDTF data type to Wikibase
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
A powerful event calendar Tool for Laravel's Nova 5.
统计信息
- 总下载量: 49.88M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1369
- 点击次数: 22
- 依赖项目数: 129
- 推荐数: 5
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-22