我有一个选择框,时间间隔为15分钟,从9点到晚上8点,使用下面的代码生成。
function hoursRange($lower = 9, $upper = 20, $step = .25, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
$timeArr = hoursRange();
$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
foreach($array as $key=>$value){
$disabled = '';
echo "<option value=".$key." $disabled>".$value."</option>";
}
echo "</select>";
它给了我一个选择框,其中包含选项9:00,9:15等,如下所示
Array
(
[0] => 9:00am
[1] => 9:15am
[2] => 9:30am
[3] => 9:45am
[4] => 10:00am
[5] => 10:15am
[6] => 10:30am
[7] => 10:45am
[8] => 11:00am
[9] => 11:15am
[10] => 11:30am
[11] => 11:45am
[12] => 12:00pm
[13] => 12:15pm
[14] => 12:30pm
[15] => 12:45pm
[16] => 1:00pm
[17] => 1:15pm
[18] => 1:30pm
[19] => 1:45pm
[20] => 2:00pm
[21] => 2:15pm
[22] => 2:30pm
[23] => 2:45pm
[24] => 3:00pm
[25] => 3:15pm
[26] => 3:30pm
[27] => 3:45pm
[28] => 4:00pm
[29] => 4:15pm
[30] => 4:30pm
[31] => 4:45pm
[32] => 5:00pm
[33] => 5:15pm
[34] => 5:30pm
[35] => 5:45pm
[36] => 6:00pm
[37] => 6:15pm
[38] => 6:30pm
[39] => 6:45pm
[40] => 7:00pm
[41] => 7:15pm
[42] => 7:30pm
[43] => 7:45pm
[44] => 8:00pm
)
注意:现在我的问题是我必须通过条件从选择框中删除一些选项
当前时间=上午11点=&gt;比之前的所有选项都被禁用
当前时间= 2pm =&gt;所有选择的选项都应该被禁用
当前时间=下午3:30或下午3:48,然后应禁用下午4点之前的所有选项。
为此,我试过这个
date_default_timezone_set('Asia/Kolkata');
$curretHour = date("g");
echo "<select>";
foreach($array as $key=>$value){
$newArr = explode(':', $value);
if(($newArr[0] == $currentHour) || $newArr[0] <$currentHour){
$dis = 'disabled = disabled';
}else{
$dis = '';
}
echo "<option value=".$key." $dis>".$value."</option>";
}
echo "</select>";
但它不起作用的任何人请帮助我。任何帮助将不胜感激。
答案 0 :(得分:0)
最好和最易读的解决方案是使用DateTime类。我建议先创建当前的DateTime。在foreach循环中,您必须创建DateTime foreach时间并与当前时间进行比较。可能的解决方案:
<?php
function hoursRange($lower = 9, $upper = 20, $step = .25, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
$timeArr = hoursRange();
$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
$currentDateTime = new \DateTime();
foreach($array as $key=>$value){
$dateTime = \DateTime::createFromFormat('g:ia', $value);
$disabled = $dateTime <= $currentDateTime;
echo "<option value=".$key." $disabled>".$value."</option>";
}
echo "</select>";
答案 1 :(得分:0)
我发现这是我问题的灵魂
--Selects-- 11:00 AM 上午11:15 上午11:30 上午11:45 12:00 PM 下午12:15 下午12:30 下午12:45 下午01:00 01:15 pm 01:30 pm 下午01:45 下午02:00 02:15 pm 02:30 pm 02:45 pm 下午03:00 03:15 pm 03:30 pm 03:45 pm 下午04:00 下午04:15 04:30 pm 04:45 pm 05:00 pm
- &GT;
<?php
//date_default_timezone_set('Asia/Kolkata');
//echo date("g");
function hoursRange($lower = 9, $upper = 20, $step = .25, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
$currentHour = 9;
$array = $timeArr = hoursRange();
//$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
$i = 0;
foreach($array as $key=>$value){
$newArr = explode('.', $key);
if(($newArr[0] == $currentHour) || $newArr[0] <$currentHour){
$disabled = 'disabled = disabled';
}else{
$disabled = '';
}
//$disabled = '';
echo "<option value=".$i." $disabled>".$value."</option>";
$i++;
}
echo "</select>";
echo "<pre>";print_r($array);
/// JS
<select id="test" name="test">
<option value="1">9:00 am</option>
<option value="2">10:00 am</option>
<option value="3">11:00 am</option>
<option value="4">8:00 pm</option>
</select>
<script>
var selectbox = document.getElementById('test');
//var opt = ['9:00 am', '10:00 am','11:00 am','8:00 pm'];
//var opt = ['00:00'=>'12:00 am','10:00'=>'10:00 am'];
var opt = {}; // no need for an array
opt["900"] = '09:00 am';
opt["1000"] = '10:00 am';
opt["1100"] = '11:00 am';
opt["1200"] = '12:00 pm';
opt["1300"] = '01:00 pm';
opt["1400"] = '02:00 pm';
var curtime = '1400';
var str = curtime.split(':');
//console.log(opt);
selectbox.onchange = function(){
//alert('here');
var i=0;
var list = '';
for (var key in opt) {
console.log('key');
if(curtime <= key){
console.log('if');
var dis = 'disabled';
console.log(opt[key]);
}
list += '<option '+dis+' value='+i+'>'+opt[key]+'</option>';
//console.log(key);
i++;
}
selectbox.innerHTML = list;
};
</script>