php-standard-library/date-time
Immutable, timezone-aware DateTime types for PHP. Provides Duration, Period, and Interval helpers for safer date/time arithmetic and ranges, designed as a standard-library style package with clear docs and contribution links.
DateTimeImmutable alternatives). Reduces side-effect risks in stateful operations (e.g., caching, event dispatching).Carbon/DateTime misuse.Period::months(1) for subscription renewals).Interval::daily() for cron-like logic).Carbon::addDays() → Duration::days(5)).now(), Carbon) but enforces stricter contracts.Carbon in most cases (e.g., use DateTime\DateTime instead of use Carbon\Carbon).function handle(Duration $duration)).Carbon in new features (e.g., API responses, validation).Carbon usages (e.g., queries, jobs) via:
CarbonAdapter to bridge old/new code).Carbon usages.Carbon (but avoid mixing).DateTime::fromTimestamp($record->created_at)).Carbon-dependent libraries (e.g., laravel/framework internals).Duration::add() vs. Carbon::addDays(). Requires team training.JsonSerializable).Carbon for critical paths (e.g., bulk date calculations).DateTime objects requires custom factories (e.g., DateTime::factory()->create()).Carbon in legacy code, new package in features)?Duration, Period, etc.@var Duration).refreshDatabase() with seeded DateTime objects.| Phase | Focus Area | Tools/Techniques | Example Refactor |
|---|---|---|---|
| Assessment | Audit Carbon usage |
git grep, PHPStan baseline |
Carbon::now() → DateTime::now() |
| New Code | Opt-in adoption | Feature flags, type hints | return new Duration($days); |
| Core Logic | High-risk areas | Wrapper classes, A/B testing | Job::dispatch(new ProcessOrder($order, $duration)) |
| Legacy | Critical paths | Polyfills, deprecation warnings | CarbonAdapter::fromDateTime($dt) |
DateTime objects and DB timestamps.// Eloquent model
protected $casts = [
'created_at' => DateTime::class,
];
Carbon, wrap DateTime objects (e.g., new Carbon($dateTime)).Carbon::createFromDateTime($dateTime)).Carbon assumptions (e.g., some analytics tools).DateTimeDTO with Carbon parsing).$request->validate(['due_date' => 'date']);
$dueDate = DateTime::fromIsoString($request->due_date);
Carbon.Carbon in Blade templates (e.g., {{ $date->format('...') }} → {{ $date->toRfc2822() }}).DateTime factories.DateTime objects with fixed timestamps.Period::years(1) is clearer than Carbon::addYears(1).Carbon::setTimezone() surprises).Duration::add() vs. Carbon::addDays()).DateTime::parse() like Carbon).Carbon methods to new package equivalents.DateTime::now() defaults to UTC (unlike Carbon’s local timezone).->toIsoString() for consistent timestamp formatting.TypeError in mixed Carbon/DateTime code.Period vs. Interval differences).Interval::weekly()).Duration::days(5)->addTo($date)).static $zeroDuration = new Duration(0)).Period arithmetic) may be slower than Carbon.
Carbon for critical paths.How can I help you explore Laravel packages today?