如果值与函数生成的值匹配,如何从下拉列表中选择值?无法选择值,例如$match value is 08:00
条件应从下拉列表中选择值?
PHP函数
function create_time_range($start, $end, $interval = '15 mins', $format = '12') {
$startTime = strtotime($start);
$endTime = strtotime($end);
$returnTimeFormat = ($format == '12')?'H:i':'H:i';
$current = time();
$addTime = strtotime('+'.$interval, $current);
$diff = $addTime - $current;
$times = array();
while ($startTime < $endTime) {
$times[] = date($returnTimeFormat, $startTime);
$startTime += $diff;
}
$times[] = date($returnTimeFormat, $startTime);
return $times;
}
$times= create_time_range('00:00', '23:45', '15 mins');
$match = '08:00';
Html
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){ ?>
<?php
if (in_array($match, $times))
{
$selected = 'selected';
}
?>
<option <?php echo $selected ?> value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
将html部分更新为以下
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){
$selected = $match===$val ? 'selected' : '';
?>
<option <?php echo $selected ?> value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>