A
A
Alex2018-02-16 17:32:18
PHP
Alex, 2018-02-16 17:32:18

How to form overlapping periods?

Greetings!
I apologize for the appeal, I have a headache with working with dates.
I need to do the following thing: if the periods intersect, then I need to separate them.
Incoming data (for example, in reality datetime objects)

[
    0 => ['openDate' => "2018-05-15", 'closeeDate' => "2018-05-30", "item" => 77],
    1 => ['openDate' => "2018-05-20", 'closeeDate' => "2018-05-25", "item" => 60]
]

In this example, the first period should be divided into two (key: 0, 2).
Those. get the output
[
    0 => ['openDate' => "2018-05-15", 'closeeDate' => "2018-05-19", "item" => 77]
    1 => ['openDate' => "2018-05-20", 'closeeDate' => "2018-05-25", "item" => 60],
    2 => ['openDate' => "2018-05-26", 'closeeDate' => "2018-05-30", "item" => 77],
]

Can you post the algorithm?
Thank you.
UPD
Breaking periods for each day is not an option. Dozens of arrays can come, in which the periods are for a year or two.
I still see the option to look for the intersection (it's simple). And how to form the required periods further. Those. detail.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Dyrkov, 2018-02-16
@sensus

As far as I understand it goes like this
5a872b932ef83349281812.jpeg

A
Alexander null, 2018-02-16
@snikes

another option is to collect everything in one array
initial data
$days = [2018-05-15, 2018-05-30, 2018-05-20, 2018-05-25];
then we sort in ascending order,
after which we take the first element and the second -1 day, then the second and 3-1 days, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question