如何创建时间下拉列表? [英] How to create a dropdown of time?
本文介绍了如何创建时间下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在PHP中创建一个下拉列表.它应该是24小时格式,间隔为30分钟,例如00:00
然后00:30
然后13:00
等等,我希望它全天候运行.
I want to create a dropdown for time in PHP. It should be of 24 hour format with an interval of 30 minutes like 00:00
then 00:30
then 13:00
and so on.I want it round the clock.
我已经使用了这段代码
<?php
$start = strtotime('12:00 AM');
$end = strtotime('11:59 PM');
?>
<select style="width:85px;" name="select1" id="select1">
<?php for($i = $start;$i<=$end;$i+=1800){ ?>
<option value='<?php echo date('G:i', $i); ?>'><?php echo date('G:i', $i); ?></option>;
<?php } ?>
</select>
问题在于,由于增量值超出结尾,因此无法覆盖11:30到12:00 AM的最后一个跨度,在PHP中有直接的方法吗?
The problem is that it doesn't cover the last span of 11:30 to 12:00 AM as the increment value exceeds the end, Is there a direct way to do this in PHP?
推荐答案
嗯,将来我们的时钟突然增加25小时的可能性极小,因此正常循环应该可以:
Uhm, it is extremely unlikely that in the future, our clocks suddenly gain a 25th hour, so a normal loop should do fine:
for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'
.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
这篇关于如何创建时间下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文