How to check if time is between two times in PHP

How to check if time is between two times in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to check if time is between two times in PHP

Handling time ranges in PHP often trips up developers because standard string comparison fails when the interval crosses midnight. A simple check like $current_time >= $sunrise && $current_time <= $sunset works perfectly for business hours, such as 9:00 AM to 5:00 PM. It breaks immediately when the start time is later than the end time, which is exactly what happens with astronomical data where sunset occurs the next calendar day relative to sunrise if you are only looking at clock times.

Consider a scenario where $sunrise is "5:42 am" and $sunset is "6:26 pm". If your script runs at 10:59 PM, a naive string comparison might incorrectly determine that the current time is before sunrise because "10:59" comes after "5:42" in some contexts, or worse, fail entirely if you try to compare strings directly. The core issue is that "6:26 pm" is numerically smaller than "10:59 pm", but logically, the period between sunrise and sunset includes all hours from morning until evening. When comparing times where the start time (sunrise) is earlier in the day than the end time (sunset), standard logic holds. However, if your data source provided a sunset time that was actually 2 AM (for a polar night scenario) and a sunrise of 6 AM, the logic flips.

The robust solution involves converting these human-readable strings into something comparable by default: timestamps. PHP's DateTime class or strtotime() functions handle the parsing of "am/pm" formats automatically. By converting the input strings into Unix timestamps (seconds since the epoch), you can perform straightforward mathematical comparisons regardless of AM/PM confusion or midnight crossings.

Here is a practical implementation using DateTime:

$current_time = "10:59 pm";
$sunrise = "5:42 am";
$sunset = "6:26 pm";

$now = new DateTime($current_time);
$start = new DateTime($sunrise);
$end = new DateTime($sunset);

// Ensure we are comparing within the same day context or handle the overnight flip
// If end time is numerically smaller than start time, it implies an overnight duration
if ($end < $start) {
    // The period crosses midnight; extend the end to tomorrow
    $end->modify('+1 day');
}

$is_between = $now >= $start && $now <= $end;

This approach relies on the fact that DateTime objects compare based on their internal integer representation. When $sunset is parsed as 6:26 PM and $sunrise as 5:42 AM, they fall on the same date by default. However, if you were working with a range where sunset precedes sunrise (e.g., winter in high latitudes), the end < start check detects this inversion. By modifying the end date to plus one day, you effectively stretch the interval to cover the night hours, allowing a timestamp comparison to work correctly.

One potential pitfall is time zones. The examples above assume all times are in the same timezone. If $current_time comes from a user device and $sunset is stored server-side without timezone context, the comparison will yield incorrect results. Always ensure that DateTime objects are instantiated with consistent timezone data, preferably using new DateTime($time, new DateTimeZone('UTC')) before converting to local time if necessary.

For Laravel developers, this logic translates seamlessly into Eloquent queries or form validators. You can pass these timestamps directly into database columns that store DATETIME types. If you are building a scheduling system where availability windows cross midnight, this normalization step—converting strings to objects and adjusting the end date—is the industry-standard pattern for avoiding off-by-one errors in temporal logic.

Our network