我想请一些帮助。我有这个下拉菜单
<select name="schedule_time">
<option value="00:00:00">00:00:00</option>
<option value="00:30:00">00:30:00</option>
<option value="01:00:00">01:00:00</option>
<option value="01:30:00">01:30:00</option>
<option value="02:00:00">02:00:00</option>
<option value="02:30:00">02:30:00</option>
<option value="03:00:00">03:00:00</option>
<option value="03:30:00">03:30:00:00</option>
<option value="04:00:00">04:00:00</option>
<option value="04:30:00">04:30:00</option>
<option value="05:00:00">05:00:00</option>
<option value="05:30:00">05:30:00</option>
<option value="06:00:00">06:00:00</option>
<option value="06:30:00">06:30:00</option>
<option value="07:00:00">07:00:00</option>
<option value="07:30:00">07:30:00</option>
<option value="08:00:00">08:00:00</option>
<option value="08:30:00">08:30:00</option>
<option value="09:00:00">09:00:00</option>
<option value="09:30:00">09:30:00</option>
<option value="10:00:00">10:00:00</option>
<option value="10:30:00">10:30:00</option>
<option value="11:00:00">11:00:00</option>
<option value="11:30:00">11:30:00</option>
<option value="12:00:00">12:00:00</option>
<option value="12:30:00">12:30:00</option>
<option value="13:00:00">13:00:00</option>
<option value="13:30:00">13:30:00</option>
<option value="14:00:00">14:00:00</option>
<option value="14:30:00">14:30:00</option>
<option value="15:00:00">15:00:00</option>
<option value="15:30:00">15:30:00</option>
<option value="16:00:00">16:00:00</option>
<option value="16:30:00">16:30:00</option>
<option value="17:00:00">17:00:00</option>
<option value="17:30:00">17:30:00</option>
<option value="18:00:00">18:00:00</option>
<option value="18:30:00">18:30:00</option>
<option value="19:00:00">19:00:00</option>
<option value="19:30:00">19:30:00</option>
<option value="20:00:00">20:00:00</option>
<option value="20:30:00">20:30:00</option>
<option value="21:00:00">21:00:00</option>
<option value="21:30:00">21:30:00</option>
<option value="22:00:00">22:00:00</option>
<option value="22:30:00">22:30:00</option>
<option value="23:00:00">23:00:00</option>
<option value="23:30:00">23:30:00</option>
</select>
有没有办法让它更有活力,而不是拥有这个庞大的HTML?
像这样的东西
<?php foreach (date('H:i:s') as $time): ?>
<option value="<?php echo $time; ?>"><?php echo $time; ?></option>
<?php endforeach; ?>
答案 0 :(得分:1)
你可以使用while循环来实现它,
<?php
$start = "00:00"; //you can write here 00:00:00 but not need to it
$end = "23:30";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
echo '<select name="schedule_time">';
while($tNow <= $tEnd){
echo '<option value="'.date("H:i:s",$tNow).'">'.date("H:i:s",$tNow).'</option>';
$tNow = strtotime('+30 minutes',$tNow);
}
echo '</select>';
?>
答案 1 :(得分:1)
最简单的方法:
<select>
<?php
for($hours=0; $hours<24; $hours++)
{
for($mins=0; $mins<60; $mins+=30)
{
$time = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).':00';
echo '<option value= "'.$time.'">'.$time.'</option>';
}
}
?>
</select>
答案 2 :(得分:0)
嘿,你可以尝试这个代码,你可以采取任何范围$ start和$ end变量。
<?php
$start = "12:00:00";
$end = "24:00:00";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
?>
<select name="schedule_time">
<?php
while($tNow <= $tEnd){ ?>
<option value="<?php echo date("H:i:s",$tNow); ?>"> <?php echo date("H:i:s",$tNow);?></option>
<?php
$tNow = strtotime('+30 minutes',$tNow);
}
?>
答案 3 :(得分:0)
尝试使用此代码。
<?php
$start = '00:00AM';
$end = '11:59PM';
$interval = '+30 minutes';
$now_str = strtotime($start);
$end_str = strtotime($end);
echo '<select name="schedule_time">';
while($now_str <= $end_str){
?>
<option value="<?php echo date('H:i A', $now_str);?>" ><?php echo date('H:i A', $now_str);?></option>
<?php
}
echo '</select>';
?>