Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Carbon Laravel Package

nesbot/carbon

Carbon is a popular PHP date/time library that extends DateTime with a fluent, human-readable API for parsing, formatting, comparing, and manipulating dates, plus localization, relative time, intervals, and test helpers. It is widely used in Laravel and standalone PHP projects.

View on GitHub
Deep Wiki
Context7

An API extension for DateTime that supports 281 different languages.

Frequently asked questions about Carbon
How do I use Carbon in Laravel without installing it via Composer?
Carbon is pre-installed with Laravel. Just use the `Carbon` facade directly in your code, like `Carbon::now()` or `Carbon::parse($dateString)`. No additional setup is required for basic usage.
What’s the difference between Carbon::parse() and new DateTime() in Laravel?
Carbon::parse() is more flexible—it handles ambiguous dates (e.g., '01/02/2023') and custom formats better than `new DateTime()`. In Laravel, prefer Carbon for readability and consistency, especially with timezones or arithmetic.
Can I use Carbon with Laravel’s Eloquent timestamps (created_at, updated_at)?
Yes! Eloquent timestamps are already Carbon instances by default. You can manipulate them directly, like `$user->created_at = Carbon::now()->subDays(1);`, or use Carbon methods in accessors/mutators.
How do I mock Carbon dates in Laravel tests (e.g., for time-sensitive logic)?
Use `Carbon::setTestNow($customDate)` to freeze time globally in tests. For example: `Carbon::setTestNow(Carbon::parse('2023-01-01'));`. Reset with `Carbon::resetTestNow()`. Works seamlessly with Laravel’s testing helpers.
Does Carbon support immutable date objects in Laravel, and why should I care?
Yes, Carbon supports immutability via `copy()` or `freeze()`. Use it to avoid unintended mutations in multi-threaded or shared-state contexts (e.g., queues, API responses). Example: `$safeDate = $date->copy()->addDays(1);`
How does Carbon handle timezones in Laravel applications with global users?
Carbon respects PHP’s default timezone but lets you override it per instance: `Carbon::parse($date)->timezone('America/New_York')`. For Laravel apps, set the server’s timezone in `.env` (`APP_TIMEZONE=UTC`) and use Carbon for user-specific adjustments.
What’s the performance impact of using Carbon vs. PHP’s native DateTime in Laravel?
Carbon adds minimal overhead (~5–10% slower for parsing) but offers far more features. For high-throughput Laravel apps (e.g., APIs), benchmark critical paths. Cache parsed dates (e.g., `Carbon::parse($input)->startOfDay()`) to mitigate costs.
Can I extend Carbon with custom methods (e.g., business-hour calculations) in Laravel?
Absolutely! Use Carbon’s macro system: `Carbon::macro('businessHours', function () { ... });`. This lets you add domain-specific logic (e.g., `Carbon::now()->businessHours()->isOpen()`) without modifying core Carbon.
How do I handle multilingual date formatting in Laravel with Carbon?
Set the locale with `Carbon::setLocale('fr_FR')` and use `format()` or `isoFormat()`. Test edge cases (e.g., `Carbon::parse('02-29-2023')` in non-Gregorian locales) in CI. Laravel’s localization system integrates well with Carbon’s locale features.
What are the risks of upgrading from Carbon 2.x to 3.x in a Laravel project?
Carbon 3.x drops PHP 7 support and tightens type safety. Audit your code for deprecated methods (e.g., `Carbon::now()->add()` → `Carbon::now()->addDays()`). Laravel 9+ uses Carbon 3.x, so upgrade incrementally and test timezone/locale edge cases.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections