php - 如何根据服务时间禁用时段?

标签 php time duration appointment timeslots

我只想根据我的营业时间计算时间段列表,并阻止禁用那些时间段预约。

我已经对我的问题做了这些事情,坚持上一个任务,在下面提到。

My Service Duration = 60min.
My Today Busines Hours:
start time - 10:00 AM
end time - 3:00 PM

我根据今天的工作时间生成了 15 分钟的时间段列表:

$start = strtotime('10:00 AM');
$end = strtotime('3:00 PM');
for( $i = $start; $i <= $end; $i += (60*15)) 
{
    $slotlist[] = date('g:iA', $i);
}

//output
10:00AM
10:15AM
10:30AM
10:45AM
11:00AM
11:15AM
11:30AM
11:45AM
12:00PM
12:15PM
12:30PM
12:45PM
1:00PM
1:15PM
1:30PM
1:45PM
2:00PM
2:15PM
2:30PM
2:45PM
3:00PM

现在我已经预约了 3 个约会

约会 |开始时间到结束时间


Appointment-1 : 10:00AM to 11:00AM (根据我的服务时长60分钟)
预约 2:上午 11:15 至下午 12:15
约会 3:下午 1:00 到下午 2:00


//array of all appointment start_time
$appointment_start_time = array('10:00AM','11:15AM','1:00PM');

现在禁用那些已经在约会中预订的时间段

foreach($slotlist as $single)
{
    if(in_array($single, $appointment_start_time))
    {
        echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";

        $time = $single;                                                
        $event_length = 60-15;          // 60 is time duration                              
        $timestamp = strtotime("$time");                                
        $endtime = strtotime("+$event_length minutes", $timestamp);     
        $next_time = date('g:i A', $endtime);                       

        //calculate between slots (start-end time)
        $start = strtotime($single);
        $end = strtotime($next_time);
            for( $i = $start; $i <= $end; $i += (60*15)) 
            {
                $list2[] = date('g:iA', $i);
            }
    }
    else
    {   
        if(in_array($single, $list2))
        {
            echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";
        }
        else
        {   

            echo "<input name=start_time type='radio'  value='$single' />".$single; echo "<br>";
        }
    }

}

//output
10:00AM -disabled
10:15AM -disabled
10:30AM -disabled
10:45AM -disabled
11:00AM
11:15AM -disabled
11:30AM -disabled
11:45AM -disabled
12:00PM -disabled
12:15PM
12:30PM
12:45PM
1:00PM -disabled
1:15PM -disabled
1:30PM -disabled
1:45PM -disabled
2:00PM
2:15PM
2:30PM
2:45PM
3:00PM

现在要做的最后一个任务, 如何删除那些不符合服务持续时间的时间段,例如:

11:00AM (single slot b/w 2 appointment, cover only 15min)

12:15PM -
12:30PM -(3 slot b/w 2 appointment, cover only 45min)
12:45PM -

最佳答案

2 天过去了,我让这件事成为可能

$slottimes = 15;
$duration = 60; 
$business_start = '10:00AM';
$business_end = '3:00PM';
echo "<b>Today's Wroking Hours $business_start to $business_end</b><br>";
echo "<b>Calendar Slot(s) Time:</b> $slottimes<br>";
echo "<b>Service Duration Time:</b> $duration<br><br>";
$start = strtotime($business_start);
$end = strtotime($business_end);
/*$endtimestamp = strtotime($business_end);
$end = strtotime("-$slottimes minutes", $endtimestamp);*/
    for( $i = $start; $i <= $end; $i += (60*$slottimes)) 
    {
        $slotlist[] = date('g:iA', $i);
    }

echo "<b>Generate Today's Available Appointment Time(s)</b><br>";
echo "<pre>"; print_r($slotlist); echo "</pre>";

echo "<b>Today's Booked Appointment(s)</b>";
$appstarttime = array('11:00AM', '11:15AM', '1:00PM', $business_end );          //business end-time always be in array
$previoustime = NULL;
$nexttime = array($business_end);

foreach($slotlist as $single)
{
    if(in_array($single, $appstarttime))
    {
        //calculate next time
        $time = $single;                                                
        $event_length = $duration-$slottimes;           // 60min Service duration time  - 15 slot time                          
        $timestamp = strtotime("$time");                                
        $endtime = strtotime("+$event_length minutes", $timestamp);     
        $next_time = date('g:i A', $endtime);                       

        //calculate between slots (start-end time)
        $start = strtotime($single);
        $end = strtotime($next_time);
            for( $i = $start; $i <= $end; $i += (60*$slottimes)) 
            {
                $nexttime[] = date('g:iA', $i);
            }



        //calculate previous time
        $time1 = $single;                                               
        $event_length1 = $duration-$slottimes;          // 60min Service duration time - 15 slot time                               
        $timestamp1 = strtotime("$time1");                              
        $endtime1 = strtotime("-$event_length1 minutes", $timestamp1);  
        $next_time1 = date('g:i A', $endtime1);                         

        //calculate between slots (start-end time)
        $start1 = strtotime($next_time1);
        $end1 = strtotime($single);
            for( $i = $start1; $i <= $end1; $i += (60*$slottimes)) 
            {
                $previoustime[] = date('g:iA', $i);
            }


    }
}
echo "<br>Appointment Start Time: ";
print_r($appstarttime);
echo "<br>";

echo "<br><strong>Previous Time: </strong>";
print_r($previoustime);
echo "<br>";

echo "<br><strong>Next Time: </strong>";
print_r($nexttime);
echo "<br>";

echo "<br><strong>Merge Both Time:</strong> ";
if($previoustime && $nexttime)
{
    $merge = array_merge($previoustime, $nexttime);
    print_r($merge);
}
echo "<br>";echo "<br>";

$flag1 = 0;
$flag2 = 0;
echo "<hr><b>Today's Available Time For Appointment:</b><hr>";
foreach($slotlist as $single)
{
    if(in_array($single, $merge))
    {
        echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";
    }
    else
    {
        echo "<input name=start_time type='radio'  value='$single' />".$single; echo "<br>";
    }

    if(in_array($single, $merge))
    {
        $flag1 = 1;
    }
    else
    {
        $flag2 = 2;
    }

}

if($flag2 != 2)
{   echo "Sorry ! Today's all appointments has been booked."; }

echo "<hr>";

关于php - 如何根据服务时间禁用时段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428768/

相关文章:

php - php 'if x && y'的处理

ruby-on-rails - Rails Activerecord/Postgres 时间格式

ios - 如何在 Spotify 播放器中向前或向后 10 秒

ios - 计算完成Apple Maps旅程所需的时间

python - 在 python 中列出两个日期之间的小时数

ios - 持续时间不随 AVPlayer currentItem 一起提供

php - curl - SPNEGO 无法找到协商机制

php - 仅在 Laravel 5.4 用户名上验证英文字母和数字

php - 如何在elastica php中一次性更新多个数据?

java - Android 比较纪元时间和今天日期并输出差异