如何根据可用时隙和预订时隙找到空闲时隙? [英] How to find free time slot based on available time slot and booked time slot?

查看:69
本文介绍了如何根据可用时隙和预订时隙找到空闲时隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户A.用户A的可用时间为10:45:00到18:45:00.表示他可以在那段时间内接受约会.

I have one user A. The User A is available for 10:45:00 to 18:45:00. means he can accept appointments during that time.

因此,假设用户A有两个预定约会:
(1)11:15:00至12:15:00和
(2)13:30:00到15:45:00

so suppose, User A has two booked appointments:
(1) 11:15:00 to 12:15:00 and
(2) 13:30:00 to 15:45:00

两次约会之间至少要有10分钟的间隔.

There is minimum 10 minutes gap between two appointment.

所以现在用户A的空闲时间是:

So now User A's free timing is:

(1)10:45:00到11:05:00(开始时间= 10:45:00,结束时间= 11:05:00,因为用户A预订了从11:15:00开始的约会,时间之间有10分钟的间隔,因此结束时间为11:05:00)

(1) 10:45:00 to 11:05:00 ( start time = 10:45:00, end time = 11:05:00 because user A have booked appointment which is start at 11:15:00, there is 10 min gap between time so end time is 11:05:00)

(2)12:25:00到13:20:00(开始时间= 12:25:00,因为第一次预订的约会在12:15:00结束,所以时间之间有10分钟的间隔,所以结束时间为12:25:00,结束时间= 13:20:00,因为第二个预定约会开始于13:30:00,时间间隔为10分钟,因此结束时间为13:20:00)

(2) 12:25:00 to 13:20:00 (start time = 12:25:00 because first booked appointment is finished on 12:15:00, there is 10 min gap between time so end time is 12:25:00, end time = 13:20:00 because 2nd booked appointment is started on 13:30:00, there is 10 min gap between time so end time is 13:20:00 )

(3)15:55:00至18:45:00(与上述相同)

(3) 15:55:00 to 18:45:00 (same as above)

表示

总时隙:
10:45:00至18:45:00

Total Time Slot:
10:45:00 to 18:45:00

预订的时间段:
11:15:00至12:15:00
13:30:00至15:45:00

Booked Time Slot:
11:15:00 to 12:15:00
13:30:00 to 15:45:00

空闲时间段:
10:45:00至11:05:00
12:25:00至13:20:00
15:55:00至18:45:00

Free Time Slot:
10:45:00 to 11:05:00
12:25:00 to 13:20:00
15:55:00 to 18:45:00

PHP CODE:

$total_time = array(
    'time' => '11:15:00',
    'end_time' => '12:15:00'
);

$gettime = $this->db->query("select `appointment_id`, `time`, `end_time` from `tbl_appointment` where `date`='".$json['date']."'");
if($gettime->num_rows()>0) 
{
    foreach($gettime->result_array() as $restime)
    {
        $booked_time[] = array(
            'time' => $restime['time'],
            'end_time' => $restime['end_time']
        );
    }
}

我不知道如何实现它,所以我没有其他代码.只是两个数组.
那么问题是基于可用时间和预订时间,如何找到空闲时间?

I have no idea about how to implement it, so i have no other code. Just two array.
So question is based on available time and booked time, how to find free time?

谢谢.

推荐答案

基本解决方案(可能不是最好的解决方案,但至少是一个开始)

Basic solution (maybe not the best, but at least a start)

这个想法:从工作日开始到工作日结束,做一系列5分钟的练习.用0填充它.每次进行约会时,将其放置在5分钟内,并以1的形式放置在数组中.现在,您可以按块知道每个人是否有空.

The idea: make an array of 5-minute blocks from workday-start to workday end. Fill it with 0. Every time an appointment is made, put it in 5-minute blocks and put it in the array as 1. Now you know per block if someone is available.

//working hours
$start='10:45:00';
$end='18:45:00';
$start=strtotime($start);
$end=strtotime($end);

//end-start divided by 300 sec to create a 5 min block
$timesegments=($end-$start)/300;
//create the blocks-array
$blocks=array_fill_keys(range(0,$timesegments,5),0);

//an appointment
$app_start='10:45:00';
$app_end='11:00:00';

//make 5 minute blocks (note that workday start is 0!)
$app_start=(strtotime($app_start)-$start)/300;
$app_end=(strtotime($app_end)-$start)/300;

//put it in the blocks-array (+2 for 10 minute break)
for($i=$app_start;$i<$app_end+2;++$i){
    $blocks[$i]=1;
    }

echo '<pre>'.print_r($blocks,true).'</pre>';

这是非常基本的,没有任何检查,但我希望您可以使用它.

This is very basic and without any checks, but I hope you can use it.

这篇关于如何根据可用时隙和预订时隙找到空闲时隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆