如何使用来自 mysql 的字符串形式的时间范围创建下拉时间范围

how to create drop dwon time range using the time range in string form from mysql

我正在以 24 小时格式保存这样的时间范围 09 : 00-12 : 3115 : 00-17 : 00。这次我是这样爆发的:

                 $time = array();
                $time = explode('-',$data['tele_timings']);$starttime='';
                $starttime = $time[0];
                $endtime = $time[1];

$data['tele_timings'] 是我从数据库中选择的时间。它的格式是我上面提到的。我尝试了很多方法,但没有创建时间范围的下拉列表。我尝试过的一种方法是:

$time_start = $starttime;
            $time_end   = $endtime;

$timestamp_start = strtotime(date('d-m-Y').' '.$time_start);
$timestamp_end   = strtotime(date('d-m-Y').' '.$time_end);
$options_array = '';
while($timestamp_start <= $timestamp_end){
$options_array.='<option value="">'.date('H:i',    $timestamp_start).'</option>';

 $timestamp_start = $timestamp_start+900; //Adds 15 minutes     
 }
echo $options_array;

你的逻辑是 correct.But 你的实际问题是,当你将字符串分解为开始时间和结束时间时,例如 09 : 00-12 : 31

star_time=09 : 00 end_time=12 : 已存储 31 个。

这不是您以正确方式进行操作所需的中间结果。 所以你需要删除 them.For 之间的所有空格这个目的使用 str_replace() 函数..根据你的代码使用:

$starttime=str_replace(' ', '', $time[0]); $endtime=str_replace(' ', '', $time[1]); 然后继续相同的逻辑。 祝你一切顺利:)