dongyi3776 2017-09-19 18:21
浏览 50
已采纳

如何覆盖重叠时隙数组

So I need to create a sort of day-per-day calendar with available times, in order for a user to be able to book a meeting with one doctor from a cabinet of multiple doctors.

I hope that this explanation is not too weird already..

Btw I use Laravel 5.5

Here's an example:

Default Schedule of the cabinet : 9:00 to 19:00

Doctor 1 says that on monday, he'll be only available from 13:00 to 15:00

Doctor 2 says that on monday, he'll be only available from 10:00 to 14:00

When I query the available timeslots :

$ids = Doctor::all()->pluck('id');
$workingSchedules = WorkingSchedule::whereIn('user_id', $ids)
                        ->orderBy('start_date')
                        ->whereDate('start_date', '=', $this->datetime->format('Y-m-d'))
                        ->get();

I get:

0 => [
    "start_date" => "2017-09-18 10:00:00"
    "end_date" => "2017-09-18 14:00:00"
]
1 => [
    "start_date" => "2017-09-18 13:00:00"
    "end_date" => "2017-09-18 15:00:00"
]

And if nothing shows up from the Database then I use the default cabinet hours.

Then I use Carbon diffInMinutes() method to construct an array of 30 minutes timeslots between those date range (that the user can select).

Anyway, for my script to work correcty I need to transform the result I showed you into this:

0 => [
    "start_date" => "2017-09-18 10:00:00"
    "end_date" => "2017-09-18 15:00:00"
]

As I only have two timeslots in this example it might be simple a solution, but I might also get an array of 10 timeslots that overlapse one another..

Can somebody help me find a elegant solution that will cover all possible case ?

Thanks a lot in advance.

  • 写回答

3条回答 默认 最新

  • ds2010630 2017-09-19 18:51
    关注

    To merge overlapping time-periods, you could use this code:

    $result = [];
    $i = -1;
    foreach ($workingSchedules as $row) {
        if ($i < 0 || $row["end_date"] > $result[$i]["end_date"]) {
            if ($i >= 0 && $row["start_date"] <= $result[$i]["end_date"]) {
                $result[$i]["end_date"] = $row["end_date"];
            } else {
                $result[++$i] = $row;
            }
        }
    }
    

    $result will then have non-overlapping periods only.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?