Extracting a time into select menus with PHP

David Carr

That begs the question how do you separate the time into seperate elements?

Making use of explode can work. Exploding where there a : so:

$parts = explode(':', '14:25:00');

This would result in an array:

[
    "14",
    "25",
    "00",
]

To use an element you can refer to its index for example $parts[0] would contain 14

Using them by their index is fine to do but kinda messy.

I prefer to give each element a name using the list function

list($hours, $minutes, $seconds) = explode(':', '14:25:00');

When using list() passing an array to it will allow you to name each index of the array in the order listed.

Putting this into practice I like to define the null versions before using list to ensure the variables will exist.

$estimatedTime = 14:25:00;

$estimatedHour = null;
$estimatedMinute = null;
$estimatedSeconds = null;

list($estimatedHour, $estimatedMinute, $estimatedSeconds) = explode(':', $estimatedTime);

Now we have the variables that can be used in select menus:

note I'm using Blade syntax here for readability.

<select name="estimatedTime_hour">
    <option value="0">Hours</option>
    @foreach(range(0,23) as $hour)
        <option value="{{ $hour }}" {{ old('estimatedTime_hour', $estimatedHour) == $hour ? 'selected=selected' : '' }}>{{ $hour }}</option>
    @endforeach
</select>

<select name="estimatedTime_minute">
    <option value="0">Minutes</option>
    @foreach(range(0,59) as $minute)
        <option value="{{ $minute }}" {{ old('estimatedTime_minute', $estimatedMinute) == $hour ? 'selected=selected' : '' }}>{{ $minute }}</option>
    @endforeach
</select>

<select name="estimatedTime_second">
    <option value="0">Seconds</option>
    @foreach(range(0,59) as $second)
        <option value="{{ $second }}" {{ old('estimatedTime_second', $estimatedSeconds) == $hour ? 'selected=selected' : '' }}>{{ $second }}</option>
    @endforeach
</select>

For each time segment I have a separate select menu. Each selection uses a range function to generate an array of items. For example, range(0,23) will return an array containing all numbers between 0 and 23.

To ensure the time segment selects the correct array index inside the loop compare the the row against the time segment:

$estimatedSeconds == $hour

When they match then print selected=selected to ensure the menu selects that time segment.

This is a simple but powerful pattern.

Fathom Analytics $10 discount on your first invoice using this link

Help support the blog so that I can continue creating new content!

Sponsor

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.