Carbon::toDateString

Formats the instance as a Y-m-d date string in its own timezone, dropping the time portion.

Signature
public function toDateString(): string
  • Added in Laravel Carbon 1.0.0

Return value

stringThe date in Y-m-d form, for example "2026-03-14", rendered in the instance's current timezone.

Changelog

  • PHP Carbon 3.0Native string return type added. The implementation still calls rawFormat('Y-m-d'), so the output stays untranslated even when a locale is set.

toDateString() is a fixed shorthand for rawFormat('Y-m-d'). It takes no arguments and has no locale-aware variant — the output is always four-digit year, dash, two-digit month, dash, two-digit day. Its siblings cover the other common shapes: toDateTimeString() gives Y-m-d H:i:s (and accepts a precision of minute, second, millisecond or microsecond), while toIso8601String() gives the ATOM form with an offset.

When to reach for it

Use it whenever a date needs to become a stable key or a query value: the right-hand side of whereDate(), a groupBy bucket, a cache key segment, a CSV column, or a <input type="date"> value. It sorts lexicographically in the same order it sorts chronologically, which is why it works so well as an array key.

What trips people up

It renders in the instance's timezone. One UTC instant late on the 14th is already the 15th in Tokyo, so grouping raw UTC timestamps produces buckets that do not match the user's calendar. Convert first with copy()->setTimezone($zone), then call toDateString().

It is also not for humans. rawFormat deliberately bypasses the translator, so setting a locale changes nothing here — use isoFormat() or translatedFormat() for display strings.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build the value for a whereDate() constraint from a Carbon instance.
  • Group a collection of timestamps into per-day buckets for a chart.
  • Populate an HTML date input, which requires exactly Y-m-d.
  • Compose a deterministic cache key such as "report:2026-03-14".

Frequently asked questions

How do I get just the date from a Carbon instance in Laravel?

Call toDateString() — it returns the Y-m-d portion as a string, for example "2026-03-14". If you need a Carbon object rather than a string, use startOfDay() to zero the time instead, or toDateTimeString() when you want the full Y-m-d H:i:s form.

Why does toDateString() show a different day than I expect?

Because it renders in the instance's own timezone, and a late-evening UTC timestamp is already the next day further east. Convert with ->copy()->setTimezone($zone) before calling it. The copy matters because Laravel's Carbon is mutable and setTimezone would otherwise change the original instance.

Can toDateString() be localised or given a different format?

No — it is hard-coded to rawFormat('Y-m-d') and takes no arguments, and rawFormat deliberately bypasses Carbon's translator. For a human-readable date use isoFormat('LL') or translatedFormat('j F Y'), which respect the locale set through Carbon::setLocale() or the application's locale.

More examples

Grouping records into per-day buckets

The Y-m-d strings become the array keys, so signup and purchase land under 2026-03-14 and refund under 2026-03-15. Because the format is zero-padded and fixed width, the keys also sort correctly as plain strings — no date parsing needed to order the buckets.

Example · php
<?php

use Illuminate\Support\Carbon;

date_default_timezone_set('UTC');

$events = collect([
    ['name' => 'signup',   'at' => '2026-03-14 08:15:00'],
    ['name' => 'purchase', 'at' => '2026-03-14 21:40:00'],
    ['name' => 'refund',   'at' => '2026-03-15 02:05:00'],
]);

$byDay = $events
    ->groupBy(fn (array $event) => Carbon::parse($event['at'], 'UTC')->toDateString())
    ->map(fn ($group) => $group->pluck('name')->all());

print_r($byDay->all());

The same three events, bucketed for a Tokyo viewer

Only the first event keeps its date: 08:15 UTC is 17:15 the same day in Tokyo. The 21:40 event is already 2026-03-15 06:40 locally, and the 02:05 event on the 15th is 11:05 on the 15th. Grouping by the raw UTC string would put the purchase on the wrong day of a Tokyo user's chart.

Example · php
<?php

use Illuminate\Support\Carbon;

date_default_timezone_set('UTC');

$timestamps = ['2026-03-14 08:15:00', '2026-03-14 21:40:00', '2026-03-15 02:05:00'];

foreach ($timestamps as $timestamp) {
    $utc = Carbon::parse($timestamp, 'UTC');
    $local = $utc->copy()->setTimezone('Asia/Tokyo');

    printf("%s UTC -> %s Tokyo\n", $utc->toDateString(), $local->toDateString());
}

Feeding a query constraint

whereDate compares only the date portion of the column, and toDateString() produces exactly the Y-m-d literal it expects. Note that the comparison happens in the database's timezone, not the user's — for a per-user daily report, convert the boundaries first and use whereBetween on two full timestamps instead. This snippet needs a booted application with a database connection.

Example · php
<?php

use App\Models\Order;
use Illuminate\Support\Carbon;

$day = Carbon::parse('2026-03-14');

$orders = Order::query()
    ->whereDate('created_at', $day->toDateString())
    ->orderBy('created_at')
    ->get();

echo $orders->count(), ' orders on ', $day->toDateString(), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.