cakephp/chronos
Chronos provides immutable date/time objects for PHP, helping prevent accidental mutations and side effects. Includes ChronosDate for calendar dates fixed at 00:00:00, plus convenient APIs inspired by Carbon but no longer extending DateTime.
ClockFactory and ClockInterface integration enables seamless dependency injection (DI) in Laravel’s service container, supporting clean architecture and testability.ChronosDate (calendar-only) and ChronosTime (time-only) reduce boilerplate for domain-specific use cases (e.g., recurring tasks, business hours).addElapsedHours() mitigate edge cases in time calculations (e.g., daylight saving transitions), critical for financial or compliance systems.Carbon usage patterns (e.g., now(), modify()). The toCarbon()/fromCarbon() adapters (if implemented) could bridge legacy code.Chronos objects to DateTime fields. Example:
protected $dates = ['created_at'];
protected $casts = ['created_at' => Chronos::class];
setTestNow() and withTestNow() simplify time-freezing in PHPUnit, reducing flakiness in time-sensitive tests.DateTime extension). Migration requires replacing Carbon::now() with Chronos::now() and updating time arithmetic.Carbon for critical paths.parse()). Custom wrappers may be needed for complex date parsing.createFromTimestamp()) require PHP 8.4+. Assess project constraints.Carbon usage, or only new features (e.g., immutable dates)?DateTime extension?Carbon that need migration?ClockFactory as a singleton or bind ClockInterface to Chronos:
$app->bind(ClockInterface::class, function () {
return new ClockFactory(config('app.timezone'));
});
Chronos facade mirroring Laravel’s Carbon facade for consistency.saving, retrieved) to convert between Chronos and DateTime:
public function getCreatedAtAttribute($value) {
return Chronos::createFromDateTime($value);
}
Carbon::setTestNow() with Chronos::setTestNow() in test suites.withTestNow() for scoped time mocking in unit tests.Carbon via feature flags or module separation.Carbon in business logic layers (services, commands).Chronos::now() and setTestNow().Carbon in controllers/views (if applicable).Chronos casts/accessors.Carbon via custom static analysis tools.parse(), createFromFormat()) to ease migration:
Chronos::parse($string); // Alias for Carbon::parse()
toCarbon()/fromCarbon() for temporary interop during migration.Carbon usage (e.g., spatie/laravel-medialibrary). Replace or patch as needed.Carbon-dependent code early.composer.json and CI/CD pipelines (PHP version, test suites).Chronos::createFromDateTime() for consistency.Chronos instances where possible (e.g., cached now() calls).Chronos objects efficiently (e.g., bulk inserts).shiftTimezone() isn’t used explicitly.addElapsedHours() prevents bugs, but edge cases may still exist. Test across DST boundaries.Carbon/Chronos) may cause subtle bugs. Use feature flags to isolate changes.Carbon vs. Chronos methods (e.g., modify() → addHours()).$date->modify('+1 day')->format('Y-m-d');
With:
$date->addDays(1)->toDateString();
$date = $date->modify() pitfalls).setTestNow() and withTestNow().Chronos::now()->startOfDay()).Carbon usage during migration.How can I help you explore Laravel packages today?