2014-09-19 67 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 
} 

?>