php carbon check if now is between two times (10pm-8am)

php carbon check if now is between two times (10pm-8am)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Carbon Check If Now Is Between Two Times (10pm–8am)

Determining if a specific moment falls within a time range that crosses midnight is one of those "simple" tasks that trips up developers because it violates the intuitive expectation that $start must be less than or equal to $end. When you define your range as 22:00 (10 PM) to 08:00 (8 AM), standard comparison logic like $now >= $start && $now <= $end immediately fails. In this scenario, Carbon handles the complexity by treating the start time as belonging to today and the end time as belonging to tomorrow.

The most robust approach avoids creating a new instance for the end time. Instead, you explicitly shift the end time forward by one day. This ensures that your comparison logic remains consistent regardless of whether the current moment is 11 PM or 5 AM.

use Illuminate\Support\Carbon;

$start = Carbon::parse('22:00:00');
$end   = Carbon::tomorrow()->parse('08:00:00');
$now   = Carbon::now('UTC');

if ($now->between($start, $end)) {
    // Logic executed between 10 PM and 8 AM
}

By using Carbon::tomorrow(), you construct a valid timeline where the start and end points are on the same day relative to each other. If it is currently Monday at 11 PM, $start represents Monday at 22:00, and $end represents Tuesday at 08:00. The between() method then correctly identifies that the current timestamp lies within this window.

If your application logic requires a reusable check rather than a one-off comparison, you might prefer storing the offset as a constant or helper method. This prevents errors where developers accidentally reuse a hardcoded end time from yesterday or tomorrow without adjusting for the current date context.

function isNightShift(Carbon $now): bool
{
    $start = Carbon::parse('22:00:00');
    $end   = Carbon::tomorrow()->parse('08:00:00');
    
    return $now->between($start, $end);
}

Handling Timezones Correctly

The example provided in the source material initializes $now using Carbon::now('UTC'). While UTC is often the safest default for server-side logic to avoid daylight saving time (DST) shifts and timezone confusion across different clients, you must ensure your comparison times are also interpreted in that same zone.

If $start and $end are parsed without a timezone argument, Carbon assumes the system's local timezone. Comparing a UTC timestamp against a local-time timestamp will yield incorrect results unless both sides of the equation share the same temporal context. Always ensure your time literals match the timezone of the $now instance you are comparing against.

Edge Cases and Precision

A common pitfall involves how between() handles exact matches. By default, the method includes the start and end times in the range (inclusive). If your business logic requires a strict "inside" check—meaning 22:00:00 exactly should be false—you would need to adjust the boundaries or use strict comparison operators.

// Inclusive by default
if ($now->between($start, $end)) { ... }

// Exclusive check (strictly between)
if ($now > $start && $now < $end) { ... }

Why This Matters

Failing to account for the midnight crossing leads to silent logic failures. For instance, a notification system might fail to trigger an alert at 1:00 AM because the code compares it against a range that thinks the day has already ended and the new one hasn't begun yet in the same frame of reference. By normalizing the end time to the next day, you create a logic gate that is resilient to date changes.

For teams managing complex scheduling or night-shift workflows, this pattern ensures reliability without requiring custom math libraries or manual date arithmetic. If you are building similar utilities for other languages or frameworks, remember that the core principle remains the same: when a range crosses midnight, extend the end date by one day to maintain linear comparison logic.

Our network