如何覆盖重叠时隙数组

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.

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.

To be easier, I will suppose $workingSchedules is an array of numbers, then we can easily compare elements

$workingSchedules = [
    [
        'start_date' => 1,
        'end_date' => 5,
    ],
    [
        'start_date' => 13,
        'end_date' => 16,
    ],
    [
        'start_date' => 16,
        'end_date' => 17,
    ],
];

$result = [$workingSchedules[0]];
$index = 0;
foreach ($workingSchedules as $row) {
    if ($result[$index]['end_date'] >= $row['start_date']) {
        $result[$index]['end_date'] = max($result[$index]['end_date'], $row['end_date']);
    } else {
        $index++;
        $result[] = $row;
    }
}

var_dump($result);

Above code will print:

[
    [
        'start_date' => 1,
        'end_date' => 5,
    ],
    [
        'start_date' => 13,
        'end_date' => 17,
    ],
]
  1. You can custom the code to compare 2 dates instead numbers
  2. If $workingSchedules is empty, we can simply return default schedule

I hope this will help.

$workingSchedules=array(
    0=>array(
    "start_date" => "2017-09-18 10:00:00",
    "end_date" => "2017-09-18 14:00:00"),
    1=>array(
        "start_date" => "2017-09-18 13:00:00",
        "end_date" => "2017-09-18 15:00:00"
    )
);



foreach ($workingSchedules as $schedule){
    $start=new DateTime($schedule['start_date']);
    $end=new DateTime($schedule['end_date']);

    while ($start<=$end){
        echo $start->format('Y-m-d H:i')."<br/>";
        $start=$start->add(new DateInterval('PT'.'30'.'M'));
    }
}