Formatting Dates
Format Carbon dates with format() and the handy toDateString helpers.
Syntax
$carbon->format('Y-m-d H:i');Carbon uses the same format characters as PHP's date(). It also provides shortcut methods:
toDateString()—2026-07-15toTimeString()—13:45:00toDateTimeString()— bothformat('...')— anything you like
Example
Loading editor…
Press Run to execute the code.
When to use it
- A PDF invoice generator formats order dates with format('d F Y') to produce human-readable strings like '14 March 2025' in the document.
- A REST API serialises all datetime fields using toIso8601String() to ensure a consistent format that front-end clients can parse reliably.
- A dashboard widget uses diffForHumans() to show 'posted 3 hours ago' instead of a raw timestamp, improving readability for end users.
More examples
Common format() patterns
Demonstrates four popular format() patterns alongside toIso8601String(), covering both human-readable and machine-readable output.
use Carbon\Carbon;
$dt = Carbon::parse('2025-03-14 09:30:00');
$dt->format('Y-m-d'); // '2025-03-14'
$dt->format('d/m/Y H:i'); // '14/03/2025 09:30'
$dt->format('D, d M Y'); // 'Fri, 14 Mar 2025'
$dt->toIso8601String(); // '2025-03-14T09:30:00+00:00'Human-readable relative time
diffForHumans() produces natural-language descriptions relative to now, with an optional short form for compact UIs.
use Carbon\Carbon;
$posted = Carbon::parse('2025-03-13 09:00:00');
echo $posted->diffForHumans(); // '1 day ago'
echo $posted->diffForHumans(short: true); // '1d ago'
$future = now()->addHours(2);
echo $future->diffForHumans(); // 'in 2 hours'Localise formatted dates
Setting a locale on Carbon makes both isoFormat() and diffForHumans() output translated month and day names automatically.
use Carbon\Carbon;
Carbon::setLocale('fr');
$dt = Carbon::parse('2025-03-14');
echo $dt->isoFormat('dddd D MMMM YYYY');
// => 'vendredi 14 mars 2025'
echo $dt->diffForHumans();
// => 'il y a 1 jour'
Discussion