How to create a dropdown of time?

How to create a dropdown of time?

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

Stefan Bogdanescu

Founder & Senior Architect

Building a Robust 24-Hour Time Dropdown in PHP

Generating a time picker dropdown that covers an entire 24-hour cycle in strict 30-minute increments is a common requirement for scheduling interfaces. The goal is often to present options like 00:00, 00:30, up through 23:30. A naive implementation using standard date arithmetic can easily break at the day boundary, specifically failing to include the final interval before midnight rolls over to the next day.

The core challenge lies in how PHP's strtotime function handles time strings without a date context. When you run $start = strtotime('12:00 AM'), PHP assumes the current system date. While this works fine for midday, relying on string-based parsing for loop boundaries is fragile. The specific issue arises when attempting to increment from 11:59 PM (or the target end time) by a fixed amount; if the loop condition checks strictly against the start of the next day without accounting for the rollover, the final 30-minute slot is often skipped.

To solve this reliably, you should treat time as a continuous integer offset rather than relying on string comparisons or strict date boundaries within a for loop. The most robust approach involves calculating the total number of intervals required for a full day and iterating that count. Since there are 24 hours in a day and each interval is 30 minutes, you have exactly 48 slots per 24-hour period ($24 \times 2 = 48$).

Here is a more deterministic approach using DateTime:

$base = new DateTime('today');
$base->modify('00:00:00'); // Start at midnight of today

$interval = new DateInterval('PT30M');
$totalSteps = 48; // 24 hours * 2 intervals per hour

<select name="time_slot" id="time_slot">
    <?php 
        for ($i = 0; $i < $totalSteps; $i++) {
            $current = (clone $base)->add($interval);
            echo '<option value="' . $current->format('H:i') . '">' . $current->format('H:i') . '</option>';
        }
    ?>
</select>

This method avoids the ambiguity of strtotime entirely. By cloning the base date object and adding intervals in a fixed loop count, you guarantee that the sequence starts at 00:00 and runs precisely 48 times. The final iteration will land on 23:30, which is exactly what is needed to cover the span up to midnight without attempting to generate 00:00 of the next day inside the same list unless explicitly desired.

If your application relies heavily on Laravel, you might find this pattern integrated into form helpers or blade components. For instance, if you are building a scheduling feature for an event management system hosted on https://laravelcompany.com, you could encapsulate this logic into a reusable helper function to keep your Blade templates clean.

Beyond the loop mechanics, consider how the data is consumed downstream. If you are storing these values in a database, ensure the column type matches the format (e.g., TIME or DATETIME). Storing just the string 09:30 is usually sufficient for dropdowns, but if you later need to compare times or calculate durations, normalizing them into Unix timestamps (Carbon objects in Laravel) is best practice.

Another consideration is the sorting order. The DateInterval approach naturally generates sorted output because it iterates linearly from the start of the day. However, if you ever modify the interval (e.g., switching to 15-minute steps), you must update the $totalSteps variable accordingly ($24 \times 4 = 96$). Hardcoding the increment value in a for($i=$start; $i<=$end) loop is prone to off-by-one errors, especially when crossing midnight.

By shifting from string-based time manipulation to integer-based iteration counts and object-oriented date handling, you eliminate the boundary condition bugs that often plague these implementations. This ensures your dropdown remains consistent regardless of the server's timezone settings or the current system clock state.

Our network