Woops... sorry about that, was doing it in a rush.
<?php
function check($num){
// To check the seconds & minutes numbers to make them
// 01, 02, 04 rather than 0, 1, 2
if($num<10 && strlen($num)<2){
$num = '0'.$num;
}
return $num;
}
function populate($name, $max){
switch($name){
// Set our hour, min & sec variables for the mktime() function
case 'hour':
$sec = $min = 0;
$format = 'g [a]';
break;
case 'min':
$sec = $hour = 0;
$format = 'i';
break;
case 'sec':
$min = $hour = 0;
$format = 's';
break;
}
$out = '<select name="'.$name.'">';
for($i=0; $i<=$max; $i++){
// Set var to $i if we're in it...
$hour = ($name == 'hour')?$i:$hour;
$min = ($name == 'min')?$i:$min;
$sec = ($name == 'sec')?$i:$sec;
$out .= '<option value="'.check($i).'">'.date($format, mktime(check($hour), check($min), check($sec), 1,1,2000)).'</option>';
}
$out .= '</select>';
return $out;
}
?>
<form name="time" action="" method="POST">
<!-- other form elements -->
Choose a time:<br>
<?php echo populate('hour', 23).' : '.populate('min', 60).' : '.populate('sec', 60); ?>
<input type="submit" name="submit" value="submitted" />
</form>
Okay, that will work. And I'll try and explain....
The first function check($num) will take the string passed to it ($num) and do two things: 1.) Check to see if it is less than 10, 2.) Check to see if the length of the string is less than 2. IF it is less than 10, and the length is less than 2, we will pre-pend a "0" so that the numbers are "00", "01", "05" instead of "0", "1", "5". It's aesthetics.
The next function, populate($name, $max), is the real worker. This takes the items passed to it and creates the drop-downs. Easy enough right? Well, we switch based upon what the value of "$name" is. If it's "hour", then we set the $min and $sec variable to 0. And the same is true for the other 2 cases. The $format variable defines how it will be shown with the date() function.
Then we start to populate the $out variable with the basic "<select name='something'>" information. Then we create a loop and add on to "$out" with each item. Each time we go through, we see what drop-down we're populating (hour, min, sec) and generate the values.
It's as easy as that. The above code is fixed, and should work perfectly.
~Brett