Split a time range into pieces by other time ranges

前端 未结 3 767
灰色年华
灰色年华 2020-12-02 01:02

I have a complicated task that I have been beating my head against the wall now for a few days. I\'ve tried about 4 different approaches, however each seems to stall and it

3条回答
  •  [愿得一人]
    2020-12-02 01:31

    The code should speak for itself:

    $original_shift[0]['start'] = '14:30:00';
    $original_shift[0]['end'] = '18:30:00';
    
    $breaks[0]['start'] = '14:30:00';
    $breaks[0]['end'] = '15:30:00';
    $breaks[1]['start'] = '16:30:00';
    $breaks[1]['end'] = '17:30:00';
    
    $modified_shift = array(
        array('start' => $original_shift[0]['start'])
    );
    
    for($x = 0, $y = count($breaks), $z = 0; $x < $y; $x++){
        $modified_shift[$z]['end'] = $breaks[$x]['start'];
        if($modified_shift[$z]['end'] != $modified_shift[$z]['start']){
            $z++;       
        }
        $modified_shift[$z]['start'] = $breaks[$x]['end'];
    }
    
    $modified_shift[$z]['end'] = $original_shift[0]['end'];
    
    if($modified_shift[$z]['end'] == $modified_shift[$z]['start']){
        unset($modified_shift[$z]);
    }
    

提交回复
热议问题