I need to write a selector of time in 30 min increments, like this:
<option value="10:00">10:00 am</option>
<option value="10:30">10:30 am</option>
<option value="11:00">11:00 am</option>
<option value="11:30">11:30 am</option>
<option value="12:00">12:00 pm</option>
<option value="12:30">12:30 pm</option>
<option value="13:00">1:00 pm</option>
<option value="13:30">1:30 pm</option>
...
<option value="23:00">11:00 pm</option>
<option value="23:30">11:30 pm</option>
One way to do it is to put this all in an array and loop through using key=>value pairs. However, I'd rather have it as a php loop.
So I found the following interesting function:
for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'
.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
It gets me close. To modify it to suit my needs I reshuffled a few things like this:
echo '<select>';
for($hours=3; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
$thrs = str_pad($hours,2,'0',STR_PAD_LEFT);
$tmin = str_pad($mins,2,'0',STR_PAD_LEFT);
$tstr = $thrs.':'.$tmin;
echo '
<option value="'.$tstr.'">
'.$tstr.' '.($thrs > 11 ? 'pm' : 'am').'
</option>';
echo '</select>';
However, something breaks and I get a single value: 23:60 pm
What am I missing?