从while到for更改循环

时间:2014-09-19 06:35:28

标签: php html

我有一个while循环,它提供从00:00到23:30的输出。但是我希望for循环得到相同的结果。

代码:

<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>

5 个答案:

答案 0 :(得分:3)

这很容易。

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

转换为

for(;strtotime($start_time) <= strtotime($end_time);)

答案 1 :(得分: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>

答案 2 :(得分:0)

<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>

答案 3 :(得分: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>

答案 4 :(得分: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 
}

?>