Hi
Anyone have a good idea for a loop that could populate a select list(form) with time values from 08:00 - 18:00 (24h time format) with an interval of 15 min between each <option value> ?

<select name='time'>
<option value='08:00'>08:00</option>
<option value='08:15'>08:15</option>
<option value='08:30'>08:30</option>
...
<option value='18:00'>18:00</option>
</select>

Kinda stuck with this one...help would be much appreciated.

    Do not know if this is considered as a "good" solution to the problem, but it works for me. Feel free to give me som other alternatives if you have a better way :-)

    function make_timelist($preval, $postname){
    echo "<select name='".$postname."' class='formstyle'>";
    echo "<option value=''>...</option>";
    	for($x=730; $x<1815; $x=($x+15)){
    		if($x < 1000){
    			$val	=	'0'.$x;
    		}else $val	=	$x;
    	$thistime	=	substr($val,0,2).":".substr($val,2,2);
    	echo "<option value='".$thistime."'>".$thistime."</option>";
    		if(substr($val,2,2)=='45'){
    			$x	=	($x+40);
    		}//end if
    	}//end for
    echo "</select>";
    }//end function make_timelist
    

      Try some arithmetic:

      $start = 8*60+0;
      $end = 18*60+0;
      
      for($time = $start; $time<=$end; $time += 15)
      {
      	$minute = $time%60;
      	$hour = ($time-$minute)/60;
      
      echo sprintf('%02d:%02d', $hour, $minute), "\n";
      }
      

      (More long-winded than it needs to be so that it's more obvious what's going on. Obviously leaves out further cosmestic stuff.)

        Thanks a lot. I agree that this is a far better solution :-)

          Write a Reply...