How to compare two time in PHP

How to compare two time in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Midnight Trap in Time Comparison Logic

Comparing two time strings like 7:30:00 and 22:30:00 to determine if the current moment falls within that window sounds straightforward. However, the logic provided in your snippet consistently returning "Close" points to a fundamental mismatch between string comparison rules and chronological reality.

The root cause lies in how PHP handles string comparisons versus date-time arithmetic. Your code uses date("G:i:s") to generate a string like "14:30:05". When you compare $time > $resttimefrom, PHP performs a lexicographical (string-based) comparison, not a temporal one. While this works fine when both times fall within the same 24-hour cycle starting from midnight, it breaks immediately if the window crosses the 12:00 AM boundary or if the logic assumes a continuous 24-hour timeline without accounting for day rollovers.

However, looking closely at your specific example where $time is always "less than" the start time or "greater than" the end time in unexpected ways, there is a more subtle issue regarding how date() formats hours versus how the comparison operators evaluate them. In many cases involving 12-hour formats (AM/PM), string comparison fails because "1:30" comes before "12:30" alphabetically even though 1 PM is after noon. But even in 24-hour format, relying on raw string comparison for time windows is brittle.

The most robust solution involves converting these human-readable strings into Unix timestamps using strtotime(). This function parses the string and returns an integer representing seconds since the Unix epoch (January 1, 1970). Once converted, you can use standard integer comparison operators to check if the current time falls strictly between your start and end times.

Here is how you should refactor the logic:

$currentTimestamp = strtotime(date("Y-m-d H:i:s"));
$startTimestamp = strtotime($resttimefrom);
$endTimestamp = strtotime($resttimeto);

if ($currentTimestamp > $startTimestamp && $currentTimestamp < $endTimestamp) {
    $stat = "open";
} else {
    $stat = "close";
}

By using strtotime(), you ensure that the comparison is based on actual time duration rather than character codes. If $resttimefrom is 23:00:00 and $resttimeto is 01:00:00, this approach still treats them as a continuous block if they are meant to span midnight, though your current logic implies a single day window. If your business logic requires handling overnight shifts (e.g., 23:00 to 07:00), you must add explicit checks for the date component or normalize the end time by adding 24 hours if it is earlier than the start time.

Another common pitfall in PHP time handling is ignoring timezone context. The date() function outputs based on the server's default timezone, while strtotime() also respects that same setting unless explicitly told otherwise. If your application serves users in different regions or runs on a server in one timezone while the business hours are defined in UTC, direct comparison will yield incorrect results. Always ensure that $resttimefrom, $resttimeto, and the current time share the same timezone context before comparing them.

For projects built with Laravel, you can leverage the now helper and Carbon (which is bundled with Laravel) to handle this elegantly without manual timestamp conversion:

use Illuminate\Support\Facades\DB; // or Carbon if installed globally

$start = Carbon::parse($resttimefrom);
$end = Carbon::parse($resttimeto);
$current = now();

if ($current->between($start, $end)) {
    $stat = "open";
} else {
    $stat = "close";
}

This approach abstracts the timestamp math and provides clear intent. It also handles timezone configuration automatically if your application's config/app.php is set up correctly. For further reading on date and time manipulation in PHP, you might explore resources like https://laravelcompany.com which often cover best practices for handling temporal data in modern stacks.

Ultimately, avoid string comparisons for time logic unless you are certain the format guarantees lexicographical order matches chronological order. Converting to timestamps or using a dedicated date library is the only way to ensure your application handles edge cases like midnight crossings and timezone shifts reliably.

Our network