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.
Illuminate\Support\CarbonImmutable). Reduces state mutation risks in critical paths like financial transactions or audit logs.Carbon::parse() ambiguity, DST edge cases). Ideal for:
Period::years(7) for immutable records).Duration/Period: Solves Laravel’s lack of native recurring payment logic (vs. CarbonPeriod’s limited functionality).Interval: Enables precise date ranges for Laravel Scout full-text search or Eloquent query scopes.Illuminate\Bus\Queue or Laravel\Echo.Date/After rules with type-safe DateTime objects.DateTime::factory()->create()).Carbon in most cases (e.g., DateTime::now() vs. Carbon::now()).function handle(Duration $duration)).Carbon code.Carbon and DateTime via adapters (e.g., CarbonAdapter::toDateTime($carbon)).DateTime serialization/deserialization.spatie/laravel-activitylog may need Carbon polyfills.Carbon-dependent utilities (e.g., Carbon::createFromFormat()) may need replacements.Duration::add() vs. Carbon::addDays() requires team training.JsonSerializable).Period calculations) could lag behind Carbon in benchmarks.nesbot/carbon’s ecosystem).DateTime objects requires custom factories (e.g., DateTime::freeze()).Carbon in legacy code, new package in features) without conflicts?DateTime::fromFormat(string $format, string $time, ?string $timezone = null)).Illuminate\Support\CarbonImmutable).Duration, Period, etc.@var Duration in method signatures).refreshDatabase() with seeded DateTime objects.| Phase | Focus Area | Tools/Techniques | Example Refactor |
|---|---|---|---|
| Assessment | Audit Carbon usage |
git grep, PHPStan baseline |
Carbon::now() → DateTime::now('UTC') |
| 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($dateTime) |
protected $casts = ['created_at' => DateTime::class].DB::raw('NOW()') to DateTime::now() in application logic.Carbon, use adapters (e.g., new Carbon($dateTime)).Carbon assumptions (e.g., some analytics tools).DateTimeDTO with Carbon parsing).$request->validate(['due_date' => 'date']);
$dueDate = DateTime::fromIsoString($request->due_date);
Carbon in shouldBeQueued() with DateTime comparisons.DateTime::now()->add(Duration::hours(1)) for delayed notifications.Carbon (e.g., phpbench).Carbon in App\Services\Billing\Subscription.Carbon in Blade templates (e.g., {{ $date->format('...') }} → {{ $date->toRfc2822() }}).DateTime factories.$factory->define(DateTime::class, fn () => DateTime::now());
DateTime objects with fixed timestamps (e.g., DateTime::freeze('2026-01-01')).Carbon::modify() side effects).Period::months(3) is clearer than Carbon::addMonths(3).DateTime::now('America/New_York')).Duration::add() vs. Carbon::addDays()).DateTime::parse() like Carbon).Carbon methods to new package equivalents.deprecated() helper for mixed Carbon/DateTime code.DateTime::now() defaults to UTC (unlike Carbon’sHow can I help you explore Laravel packages today?