1

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?

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
santa
  • 12,234
  • 49
  • 155
  • 255

3 Answers3

0

You need to wrap your loops in brackets.

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);
        $hrs = $thrs;
        if ($thrs > 12) $hrs = $thrs - 12;
        $tstr = $hrs.':'.$tmin;

        echo '
        <option value="'.$tstr.'">
              '.$tstr.' '.($thrs > 11 ? 'pm' : 'am').'
        </option>';
    }
}
echo '</select>'; 

The original code you did was equivalent to 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>'; 
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
0

Why don't use do it on client side using javascript? Beside that, your PHP code lacks of code block for the for loops

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>'; 
huydq5000
  • 274
  • 1
  • 8
0

If i understand properly you forgot curly brackets {} in for loop. Because you iterate nested for loop so it is mandatory to set {} brackets.

Solution 1 (With curly brackets):

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'
        // Do stuff.
    }
}

Solution 2 (Alternate Read This):

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'
        // Do stuff.
    endfor;
endfor;
Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39