Carbon::startOfDay

Resets the time on a Carbon instance to 00:00:00 on the same date.

Signature
public function startOfDay(): static
  • Added in Laravel Carbon 1.0

Return value

staticThe same instance with its time set to 00:00:00.000000 ($this for the mutable Carbon class, a new instance for CarbonImmutable).

Changelog

  • PHP 2.0Microseconds are zeroed as well as seconds; the generic startOf('day') form and unit enums were added alongside the named methods.

startOfDay() keeps the date and clears the time, giving 00:00:00.000000. It is how you turn "a moment" into "a day" — the normalisation step that makes range queries, day comparisons and day counts behave predictably.

Where it earns its keep

Two timestamps on the same calendar date are not equal, and diffInDays() between them returns a fraction rather than a whole number. Applying startOfDay() to both sides removes the time-of-day noise, so comparisons answer the question you actually asked.

The family

The same pattern covers the other units: startOfHour(), startOfWeek(), startOfMonth(), startOfQuarter(), startOfYear(), plus the generic startOf('hour'). startOfWeek() depends on the locale's first day of the week — Monday for most Carbon locales — and accepts an explicit day when you need to pin it.

It mutates

On the mutable Carbon class it rewrites the instance and returns the same object. On a model attribute that means the underlying value is changed and may be saved back; call copy() first, or use CarbonImmutable.

For the upper bound of a range, prefer the start of the next day over endOfDay(), whose 999999 microseconds can fall outside a column with second precision.

Example

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

When to use it

  • Normalising two timestamps before comparing them by calendar day.
  • Building the inclusive lower bound of a date-range query.
  • Turning a user-picked datetime into a clean date before storing it.

Frequently asked questions

How do I get midnight for a date I already have?

Call ->startOfDay() on it, which sets the time to 00:00:00.000000 while keeping the date. Use ->copy()->startOfDay() when the original instance must keep its time, because the mutable Carbon class modifies in place.

Should I use endOfDay() for the end of a range?

Prefer the start of the next day with a strict less-than comparison, because endOfDay() carries 999999 microseconds that a second-precision column cannot represent. A between comparison against 23:59:59.999999 can silently drop rows stored in the final second.

Which day does startOfWeek() use?

The first day of the current locale's week, which is Monday for Carbon's default English locale. Pass a day constant such as startOfWeek(Carbon::SUNDAY) when the boundary must not depend on the locale.

More examples

A safe date range for a query

The half-open range covers both end days completely without relying on end-of-day microseconds, which a DATETIME column with second precision would round or reject. Whatever times the user's inputs carried are discarded before the query is built.

Example · php
<?php

use App\Models\Order;
use Carbon\Carbon;

$from = Carbon::parse('2026-03-01 14:22:00')->startOfDay();
$to = Carbon::parse('2026-03-14 09:41:07')->startOfDay()->addDay();

$orders = Order::query()
    ->where('placed_at', '>=', $from)
    ->where('placed_at', '<', $to)
    ->get();

echo $from->toDateTimeString(), ' -> ', $to->toDateTimeString(), PHP_EOL;
// 2026-03-01 00:00:00 -> 2026-03-15 00:00:00

Comparing by calendar day

copy() keeps both originals intact, which the final line confirms — without it the first instance would be sitting at midnight afterwards. When the only question is 'same day?', isSameDay() says so directly and mutates nothing.

Example · php
<?php

use Carbon\Carbon;

$a = Carbon::parse('2026-03-14 08:00:00');
$b = Carbon::parse('2026-03-14 22:30:00');

var_dump($a->equalTo($b));                                        // bool(false)
var_dump($a->copy()->startOfDay()->equalTo($b->copy()->startOfDay())); // bool(true)
var_dump($a->isSameDay($b));                                      // bool(true)
var_dump($a->toDateTimeString());                                 // string(19) "2026-03-14 08:00:00"

Other units follow the same shape

startOfWeek() defaults to the locale's first day, Monday in Carbon's English locale, and takes an explicit day constant when a Sunday-start week is required. Every start-of method also clears the smaller units below it.

Example · php
<?php

use Carbon\Carbon;

$date = Carbon::parse('2026-03-14 18:05:09'); // Saturday

echo $date->copy()->startOfWeek()->toDateString(), "\n";                    // 2026-03-09
echo $date->copy()->startOfWeek(Carbon::SUNDAY)->toDateString(), "\n";      // 2026-03-08
echo $date->copy()->startOfQuarter()->toDateString(), "\n";                 // 2026-01-01
echo $date->copy()->startOfMinute()->format('H:i:s'), "\n";                 // 18:05:00

Discussion

  • Be the first to comment on this lesson.