If anyone manages to help me fix this i'll be more than happy. :)

SO my problem is this, i have a drop-down menu of different times of day. e.g. 00:00; 00:15; 00:30; 00:45; 01:00; 01:15 up to midnight. What i want to do is load the drop down with values which are in a range of 2 values. (these two values are stored in a database)

e.g. If the two values in the database are 05:00 and 10:00, i want to load all the available times in that range, i.e. 05:00; 05:15; 05:30; 05:45; 06:00 up till 10.


What do you think guys?

Hi Luke,

What you're asking sounds simple enough. All you need to do is use two for loops to iterate through the various options.

E.g.

$startTime = '06:30';
$endTime   = '10:45';
$options   = array();

// Split start and end times using colon as delimiter
$startTime = explode(':', $startTime);
$endTime   = explode(':', $endTime);

// Iterate through hour and minute options between start and end times
for($hour = (int)$startTime[0]; $hour <= (int)$endTime[0]; $hour++) {
    for($minute = (int)$startTime[1]; $minute <= (int)$endTime[1]; $minute += 15) {
        $optionHour = str_pad($hour, 2, '0', STR_PAD_LEFT);

        $minute = (60 == $minute) ? 0 : $minute;
        $optionMin  = str_pad($minute, 2, '0', STR_PAD_LEFT);

        $option = "{$optionHour}:{$optionMin}";
        $options[$option] = $option;
    }
}

R.

Hi R.
Thanks for your quick reply. It works but not as expected. Using the values that you used, in the second loop it will only loop from 30 to 45, therefore it starts at 6:30 then 6:45 but then it does not go to 7:00. Instead it goes to 7:30 and then again 7:45. The HOUR loop is perfect. Only the minute loop needs a bit of refining.

My mistake. Try this:

$startTime = '06:30';
$endTime   = '10:45';
$options   = array();

// Split start and end times using colon as delimiter
$startTime = explode(':', $startTime);
$endTime   = explode(':', $endTime);

// Iterate through hour and minute options between start and end times
for($hour = (int)$startTime[0]; $hour <= (int)$endTime[0]; $hour++) {
    $startMinute = ($hour == (int)$startTime[0]) ? (int)$startTime[1] : 0;
    $endMinute   = ($hour + 1 == (int)$endTime[0]) ? (int)$endTime[1] : 45;

    for($minute = $startMinute; $minute <= $endMinute; $minute += 15) {
        $optionHour = str_pad($hour, 2, '0', STR_PAD_LEFT);
        $optionMin  = str_pad($minute, 2, '0', STR_PAD_LEFT);

        $option = "{$optionHour}:{$optionMin}";
        $options[$option] = $option;
    }
}

R.

Hi R. Just been working some more on it and managed to fix it in a bit of different way.

Let's say I don't want to use a specific start time and specific end time to populate the drop-down, I just use an array which has all the available times of day from 00:00 to midnight of the same day.

So what I did is instead of storing the values of the start time and end time, I actually store the position of the start time and end time.

e.g. if I want to start the drop-down from 00:15 up to 01:30 from an array that stores values such as 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30 etc.,
all I do is store the position of the start position 00:15 (1) and the end position 01:30 (6).

Then just a simple loop which starts at 1 to 6 should display the values of the array.

echo "<select name=\"drpTimes\" id=\"drpTimes\">";
for($i=1;$i<=6;$i++)
{
echo "<option value=\$option_values[$i]\">$option_values[$i]</option>";
}
echo "</select>";

Thanks for your time R. If you wish to still answer the question for other's to see would also be great. Thanks again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.