2014-09-19 108 views
0

我有一個while循環,它給出從00:00到23:30的輸出。但我想要與for循環的結果相同。從while更改循環到

代碼:

<select> 
<?php 
    $start_time = "00:00:00"; 
    $end_time = "23:00:00"; 
    while(strtotime($start_time) <= strtotime($end_time)){?> 
     <option value="<?php date("H:i:s", strtotime($start_time))?>"> <?php echo date("H:i A", strtotime($start_time))?></option> 
     <?php $start_time = date("H:i:s", strtotime("$start_time + 15 minutes")); 
    } 

?></select> 
+0

不會你需要'while'或'for'工作數組...? – webeno 2014-09-19 06:37:46

+1

爲什麼你想改變它,如果它的工作? while循環可能是更好的解決方案。 – TiMESPLiNTER 2014-09-19 06:38:11

+0

爲什麼你想去for循環?你將不會從中得到好處。 – 2014-09-19 06:40:08

回答

3

這很容易。

while(strtotime($start_time) <= strtotime($end_time)) 

轉變爲

for(;strtotime($start_time) <= strtotime($end_time);) 
0
<select> 
<?php 
    for($start_time="00:00:00",$end_time = "23:00:00";strtotime($start_time) <= strtotime($end_time); $start_time = date("H:i:s", strtotime("$start_time + 15 minutes"))) 
    { 
?> 
     <option value="<?php date("H:i:s", strtotime($start_time))?>"> <?php echo date("H:i A", strtotime($start_time))?></option> 
     <?php 
    } 
?> 
</select> 
1
<select> 
<?php 
    $start_time = "00:00:00"; 
    $end_time = "23:00:00"; 
    for($time = strtotime($start_time); $time <= strtotime($end_time); $time += 900) { 
    ?> 
     <option value="<?php date("H:i:s", $time)?>"> 
     <?php echo date("H:i A",$time)?> 
     </option> 
     <?php 
    } 

?> 
</select> 
0

這應該爲你工作。

<select> 
<?php 
    $start_time = "00:00:00"; 
    $end_time = "23:00:00"; 
    for($i = strtotime($start_time); $i <= strtotime($end_time); $i += 900) { 
?> 
     <option value="<?php date("H:i:s", $i)?>"><?php echo date("H:i A",$i)?></option> 
<?php 
    } 

?> 
</select> 
0
<?php 

$start_time = "00:00:00"; 
$end_time = "23:00:00"; 
for($i=strtotime($start_time);$i <= strtotime($end_time);$i+=15*60){?> 
    <option value="<?php date("H:i:s", $i)?>"> <?php echo date("H:i A", $i)?></option> 
    <?php 
} 

?>