Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Learn the difference between Carbon and CarbonImmutable in Laravel, the subtle date bugs mutability causes, and how to switch your app to immutable dates globally.
If you've worked with dates in Laravel for any length of time, you've probably hit a bug like this: you calculate an end date, and somehow your start date changes too. You stare at the code, everything looks right, and yet the numbers are wrong.
The culprit is almost always mutability. In this article, we'll look at the difference between Carbon and CarbonImmutable , why the default can bite you, and how to make immutable dates the standard across your Laravel app.
Carbon is the PHP date library Laravel uses under the hood. Every time you call now() , today() , or access a created_at timestamp on a model, you're getting a Carbon instance. It extends PHP's native DateTime class and adds hundreds of friendly helpers like addDays() , startOfMonth() , diffForHumans() and more.
Carbon comes in two flavours:
Both share the same API, so the difference is invisible until it causes a problem.
Also Read: News and Google Chrome
Here's the scenario that catches almost everyone:
$start = now();
$end = $start->addDays(7);
echo $start->toDateString(); // 2026-09-29
echo $end->toDateString(); // 2026-09-29You expected $start to be today and $end to be next week. Instead, both hold the same date.
That's because addDays() on a mutable Carbon instance modifies the object in place and then returns $this . So $start and $end are literally the same object. There's only one date in memory, and you just moved it forward a week.
This gets worse when dates are passed around your application:
public function trialEndsAt(Carbon $signupDate): Carbon
{
return $signupDate->addDays(14);
}
$user->trial_ends_at = $this->trialEndsAt($user->signed_up_at);
// $user->signed_up_at has now been silently changed tooThe method looks harmless, but it has a side effect on the caller's data. These bugs are hard to track down because the code that breaks is often far away from the code that caused it.
With mutable Carbon, the fix is to copy the instance before changing it:
$start = now();
$end = $start->copy()->addDays(7);
echo $start->toDateString(); // 2026-09-22
echo $end->toDateString(); // 2026-09-29This works, but it relies on you (and every developer on your team) remembering to call copy() every single time. Forget once, and the bug is back.
Also Read: Programming: EU Cyber Resilience
With CarbonImmutable , every modification returns a brand-new instance. The original never changes:
use Carbon\CarbonImmutable;
$start = CarbonImmutable::now();
$end = $start->addDays(7);
echo $start->toDateString(); // 2026-09-22
echo $end->toDateString(); // 2026-09-29No copy() , no surprises. The code does exactly what it reads like.
One important gotcha: because immutable methods return a new object, you must assign the result. This line does nothing useful:
$date = CarbonImmutable::now();
$date->addDay(); // result is thrown away
$date = $date->addDay(); // correctIf you're converting existing code from mutable to immutable, look out for these "fire and forget" calls, since they were relying on the mutation.
| Carbon (mutable) | CarbonImmutable | |
|---|---|---|
| addDays(), subMonth() etc. | Changes the original, returns $this | Returns a new instance |
| Need copy() before modifying? | Yes, to be safe | No |
| Safe to pass into functions? | Can be changed by callee | Always safe |
| Must assign the result? | No | Yes |
| Laravel default for now() | Yes | Opt-in |
Out of the box, Laravel's now() and today() helpers return Illuminate\Support\Carbon , which extends the mutable Carbon\Carbon . Model attributes cast with datetime or date are also mutable.
So unless you've changed anything, your Laravel app is using mutable dates everywhere.
Also Read: News: IBM and NASA
Laravel lets you swap the underlying date class with a single line using the Date facade. Add this to the boot method of your AppServiceProvider :
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
public function boot(): void
{
Date::use(CarbonImmutable::class);
}Now now() , today() , and Eloquent's date attributes all return CarbonImmutable instances across your entire application.
This is one of those small changes that quietly prevents a whole category of bugs. If you're starting a new project, it's well worth adding on day one.
If you're not ready to switch globally, you can opt in per attribute using Laravel's immutable casts:
protected function casts(): array
{
return [
'published_at' => 'immutable_datetime',
'birthday' => 'immutable_date',
];
}These return CarbonImmutable instances for those attributes only, which is a good way to migrate an existing codebase gradually.
Also Read: Meta Launches Muse, an AI Agent That Acts for You - News
Sometimes you'll receive one type and need the other, for example when a third-party package expects a mutable Carbon . Both classes have conversion helpers:
$immutable = now()->toImmutable();
$mutable = $immutable->toMutable();If you type-hint Carbon\Carbon in your methods and then switch to immutable dates globally, you'll get type errors. To keep your code flexible, type-hint against the shared interface instead:
use Carbon\CarbonInterface;
public function isExpired(CarbonInterface $date): bool
{
return $date->isPast();
}CarbonInterface is implemented by both Carbon and CarbonImmutable , so your methods work regardless of which one you're given. If you want to accept any PHP date object, DateTimeInterface works too.
Laravel's time helpers like $this->travelTo() and $this->freezeTime() work the same whether you use mutable or immutable dates, so switching won't break your time-based tests. If anything, immutable dates make tests more predictable because fixtures can't be accidentally modified mid-test.
Honestly, there are very few good reasons today. The main ones are:
For everything else, immutable is the safer default.
Also Read: Shopify Ditches React Native for Swift and Kotlin
Yes, for new projects. Add Date::use(CarbonImmutable::class) to your AppServiceProvider and never think about copy() again.
For existing projects, migrate carefully. Search your codebase for date modifications that don't assign their result (like $date->addDay(); on its own line), update type-hints to CarbonInterface , and run your test suite. Alternatively, start with immutable_datetime casts on individual models and expand from there.
Immutable dates are one of the simplest upgrades you can make to a Laravel codebase. A single line in a service provider removes an entire class of subtle, time-wasting bugs.
Found this helpful? Check out more Laravel tips and tutorials on TheWebTier .