0

Class A Time:

Day 1 => 08:00-09:00 | 11:00-12:00 | 13:00-15:00 | 20:00-21:00
Day 2 => 12:00-13:00 | 14:00-15:00 
Day 3 => 08:00-09:00 | 16:00-17:00 | 18:00-19:00
Day 4 => 10:00-11:00 | 12:00-14:00 | 14:00-16:00
Day 5 => 08:00-10:00 | 13:00-15:00
Day 6 => 80:00-09:00 | 10:00-11:00

Class B Time:

Day 1 => 10:00-11:00 | 17:00-19:00
Day 2 => 08:00-09:00 | 11:00-12:00
Day 3 => 08:00-09:00 | 17:00-18:00 | 20:00-21:00
Day 4 => 08:00-09:00 | 14:00-16:00 | 17:00-18:00
Day 5 => 08:00-10:00 | 14:00-15:00
Day 6 => 10:00-11:00

How to check the two class time conflict

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 2
    Your question is unclear, please provide more / better details about your intent. What is your expected outcome given your input arrays? – mickmackusa Jul 31 '17 at 13:52
  • 1
    Would say it's still unclear. Are you expecting to evaluate if `$stime1[0] > $stime2[0]`? Or do you wish to add up all of the hours (e.g. 08:00 + 11:00 = 19:00, and so forth) and then compare them? ... How about you give the results you wish, with the starting point (which you have, so leave that there) and then also show us how/what you've tried to accomplish the result? That would be helpful to us helpful souls ;) – rkeet Jul 31 '17 at 20:38
  • @InformationArtist I wasn't intimidated by the complexity of your original posting, I am just wanting to see some more clarity on your desired result. I am not a speed-poster, I like the challenging questions. Here's a answer with some similarities: https://stackoverflow.com/a/45156381/2943403 Now, given your two separate batches of input data, what is your expected result for each? – mickmackusa Jul 31 '17 at 21:59
  • Class A Time: Day 1 08:00-09:00 | 13:00-15:00 | 20:00-21:00 Day 2 12:00-13:00 | 14:00-15:00 Day 3 08-09 | 16:00-17:00 | 18:00-19:00 Day 4 10:00-11:00 | 12:00-14:00 | 14:00-16:00 Day 5 08:00-10:00 | 13:00-15:00 Day 5 09:00-11:00 Class B Time: Day 1 10:00-11:00 | 17:00-19:00 Day 2 08:00-09:00 | 11:00-12:00 Day 3 08:00-09:00 | 17-18 | 20-21 Day 4 08:00-09:00 | 14:00-16:00 | 17:00-18:00 Day 5 08:00-10:00 | 14:00-15:00 Day 5 10:00-11:00 @mickmackusa How to check the two class multi time conflict – Information Artist Aug 01 '17 at 07:36
  • Sorry, I ran out of time today, I will try to get something posted tomorrow. So clashes are: Day3: 8-9 & Both classes on Day 5 & Day 6 10-11. I am sure I can work something out. (p.s. typo on Day 6 Class A -- but I know you don't mean `80` hundred hours). – mickmackusa Aug 01 '17 at 12:03
  • Thanks ur response, I hope u will b give the solution – Information Artist Aug 01 '17 at 12:34
  • Your previous edits displayed your coding attempt (kind of) which qualified your question to be answered. Please edit your question to include your coding attempt so that I am not frowned upon for answering a question without a coding attempt. – mickmackusa Aug 02 '17 at 23:58

1 Answers1

0

Because overlapping periods may not be a simple == comparison, class periods must be split into start and end chunks and individually processed. Unfortunately, this makes for a loop-heavy block of code -- but it is unavoidable.

array_walk_recursive() is used to prepare your input data. The function, by design, only iterates the "leaf-nodes" of $sched -- this means that $v will never be an array, it will always be a time-span string. The function is also "depth-ignorant", so the only way properly modify the original $sched data is to make $v modifiable by reference (see: &$v).

Once the data is restructured, it is time to get loopy. A series of 3 successive loops traverse the "targeted" Class and Day subarrays for comparison. A further 2 loops are used to access the same Day subarrays (periods) of all other Classes. Earlier code builds implemented array_walk_recursive() again to reduce total loops, but this is not best practice because using a foreach loop with an early exit with break(2) saves iterations and improves efficiency.

Assuming that some classes may not have a day with period, I have baked in a Day check to ensure that my method doesn't cause Notices/Warnings based on missing Day subarrays.

Code: (Demo)

$sched=[
    'Class A'=>[
        'Day 1'=>['10:00-11:00','11:00-12:00','13:00-15:00','20:00-21:00'],
        'Day 2'=>['12:00-13:00','14:00-15:00'],
        'Day 3'=>['08:00-09:00','16:00-17:00','18:00-19:00'],
        'Day 4'=>['10:00-11:00','12:00-14:00','14:00-16:00'],
        'Day 5'=>['08:00-10:00','13:00-15:00'],
        'Day 6'=>['08:00-09:00','10:00-11:00']
    ],
    'Class B'=>[
        'Day 1'=>['09:30-10:30','17:00-19:00'],
        'Day 2'=>['08:00-09:00','11:00-12:00'],
        'Day 3'=>['08:00-09:00','17:00-18:00','20:00-21:00'],
        'Day 4'=>['08:00-09:00','14:00-16:00','17:00-18:00'],
        'Day 5'=>['08:00-10:00','14:00-15:00'],
        'Day 6'=>['10:00-11:00']
    ],
    'Class C'=>[
        'Day 1'=>['12:00-14:00']
    ]
];
// Preparation: Split the dashed time spans into chunked subarrays containing start and end times
// e.g. $sched['Class A']['Day 1'][['10:00','11:00'],['17:00','19:00']]
array_walk_recursive($sched,function(&$v){$v=explode('-',$v);});

// Comparisons:
foreach($sched as $class=>$days){
    $other_classes=array_diff_key($sched,[$class=>'']);  // same array without target class
    foreach($days as $day=>$spans){
        //echo "\n\n$class, $day";
        foreach($spans as $period=>$span){
            //echo "\nchecking {$span[0]} & {$span[1]}";
            foreach($other_classes as $other_class=>$other_days){
                if(isset($other_days[$day])){  // this avoids Notices/Warning about non-existent day in class
                    foreach($other_days[$day] as $other_period=>$other_span){  // compare other classes on same day
                        //echo "\nversus {$other_span[0]} & {$other_span[1]}";
                        if(($span[0]>=$other_span[0] && $span[0]<$other_span[1]) || ($span[1]<=$other_span[1] && $span[1]>$other_span[0])){
                            $result[]="Clash: $class,$day#".++$period.": {$span[0]}-{$span[1]} vs $other_class,$day#".++$other_period.": {$other_span[0]}-{$other_span[1]}";
                            break(2);  // no need to further check this $span for clashes
                        }
                    }
                }
            }
        }
    }
}
echo implode("\n",$result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136