i have below array
Array
(
[0] => Array
(
[from_time] => 15:00
[to_time] => 17:15
)
[1] => Array
(
[from_time] => 10:00
[to_time] => 12:15
)
[2] => Array
(
[from_time] => 09:00
[to_time] => 11:15
)
[3] => Array
(
[from_time] => 09:00
[to_time] => 11:15
)
[4] => Array
(
[from_time] => 14:00
[to_time] => 16:15
)
[5] => Array
(
[from_time] => 15:00
[to_time] => 17:15
)
)
i want to get common time in this array.
If i Find common time in this array my expected result should be
Array
(
[0] => Array
(
[from_time] => 10:00
[to_time] => 11:15
)
[1] => Array
(
[from_time] => 15:00
[to_time] => 16:15
)
)
I have try with below code but not getting accurate result.
foreach ($booking_time as $time1) {
foreach ($booking_time as $time2) {
{
if(($time1['from_time'] > $time2['from_time'] && $time1['from_time'] < $time2['to_time'])|| ( $time1['to_time'] > $time2['from_time'] && $time1['to_time'] < $time2['to_time'])){
echo $time1['from_time'];
echo $time1['to_time'];
}
}
}
}
Quick example (I think it can be shorter, but for quick understanding it is ok) :
$intervals = [
['from_time' => '15:00', 'to_time' => '17:15'],
['from_time' => '10:00', 'to_time' => '12:15'],
['from_time' => '09:00', 'to_time' => '11:15'],
['from_time' => '09:00', 'to_time' => '11:15'],
['from_time' => '14:00', 'to_time' => '16:15'],
['from_time' => '15:00', 'to_time' => '17:15'],
];
$overlaps = [];
foreach ($intervals as $interval) {
$key = null;
foreach ($overlaps as $_key => $_intervals) {
foreach ($_intervals as $_interval) {
if (
($_interval['from_time'] <= $interval['from_time'] && $interval['from_time'] <= $_interval['to_time'])
||
($_interval['from_time'] <= $interval['to_time'] && $interval['to_time'] <= $_interval['to_time'])
) {
$key = $_key;
break 2;
}
}
}
if (is_null($key)) {
$key = count($overlaps);
}
$overlaps[$key][] = $interval;
}
foreach ($overlaps as &$overlap) {
$from = '00:00';
$to = '23:59';
foreach ($overlap as $_interval) {
$from = max($from, $_interval['from_time']);
$to = min($to, $_interval['to_time']);
}
$overlap = ['from_time' => $from, 'to_time' => $to];
}
unset($overlap);
print_r($overlaps);
Output :
Array
(
[0] => Array
(
[from_time] => 15:00
[to_time] => 16:15
)
[1] => Array
(
[from_time] => 10:00
[to_time] => 11:15
)
)
you wanna increment or += [any number]
$latest = aray_pop($booking_time); // this will grab the last element from the array
for($i = 0; $i < $latest; $i += 15 // this is the number you want to increment by) {
$booking_time[$key] = $i;
}
General Overlay for comparing A and B
X) A(start) < B(start) < A(end) < B(end)
Y) B(start) < A(start) < B(end) < A(end)
(and usually full inclusions are required as well)
B(start) <= A(start) < A(end) <= B(end)
A(start) <= B(start) < B(end) <= A(end)
You actually wrote that down, but you overlooked that A and B are switching places in your source (A will be B and B A at another run -> you only need to check X or Y) and depending on which case you check you have to adjust your variable/key selection.
Additionally you are not handling inclusions well (exluding them or appropriately including them).
Simple example:
a = 10-20 b = 15-25
you would expect 15-20 as result
lets assume the run: time1 a, time2 b
$time1['to_time'] > $time2['from_time'] &&
$time1['to_time'] < $time2['to_time']
matches and prints
10-20 (time1 (a) from and to -- you wanted to print time2 from and time1 to)
another run will assign: time2 a and time1 b
$time1['from_time'] > $time2['from_time'] &&
$time1['from_time'] < $time2['to_time'])
matches and prints
15-25 (time1 (b) from and to -- you wanted to print time1 from and time2 to)
Here a working example
foreach($array as $key => $time1) {
foreach($array as $key2 => $time2) {
if (($time1["from_time"] < $time2["from_time"]) && ($time2["from_time"] < $time1["to_time"]) && ($time2["to_time"] > $time1["to_time"])) {
$overlap[] = array("from_key" => $time2["from_time"], "to_time" => $time1["to_time"]);
}
//inclusion
if (($time1["from_time"] >= $time2["from_time"]) && ($time2["to_time"] >= $time1["to_time"]) && ($key !== $key2)) {
$overlap[] = array("from_time" => $time1["from_time"], "to_time" => $time1["to_time"]);
}
}
}
Disclaimer: There are no checks for double entries. If you dont want inclusions you need a way to handle A(start) < B(start) < A(end) == B(end) etc.