public static function combineDates($origin_dates, $exclusions) {
    if (empty($exclusions) || !is_array($exclusions)) {
      return $origin_dates;
    }

    foreach ($exclusions as $key => $exclusion) {
      $exclusions[$key] = [
        'from' => new \DateTime($exclusion['from']),
        'to' => new \DateTime($exclusion['to']),
      ];
    }

    $start = new \DateTime($origin_dates[0]['from']);
    $end = new \DateTime($origin_dates[0]['to']);

    $resultingPeriods = [];
    $resultingPeriods[] = ['from' => clone $start];

    $prevSkipInstance = FALSE;

    while ($start < $end) {

      // Check if this instance should be excluded.
      $skipInstance = FALSE;
      foreach ($exclusions as $exclusion) {
        if ($start >= $exclusion['from'] && $start <= $exclusion['to']) {
          $skipInstance = TRUE;
          break;
        }
      }

      // We just hit exclusion. Need to close previous resulting period.
      if ($skipInstance && !$prevSkipInstance) {
        $period = array_pop($resultingPeriods);
        $close = clone $start;
        $close->modify('-1 week');
        $close->setTime($end->format('H'), $end->format('i'));
        $period['to'] = $close;
        array_push($resultingPeriods, $period);
      }

      // If exclusion just finished. We need to start new period.
      if (!$skipInstance && $prevSkipInstance) {
        $period = ['from' => clone $start];
        array_push($resultingPeriods, $period);
      }
      $start->modify('+1 week');
      $prevSkipInstance = $skipInstance;
    }

    // Do not forget to close last period.
    $period = array_pop($resultingPeriods);
    $period['to'] = $end;
    array_push($resultingPeriods, $period);

    // Now convert dates back to strings.
    $result_dates = [];
    foreach ($resultingPeriods as $period) {
      // Example format 2018-01-08T05:15:00
      $result_dates[] = [
        'from' => $period['from']->format('Y-m-d\TH:i:s'),
        'to' => $period['to']->format('Y-m-d\TH:i:s'),
      ];
    }

    return $result_dates;
  }
public static function combineDates($origin_dates, $exclusions) {
    if (empty($exclusions) || !is_array($exclusions)) {
      return $origin_dates;
    }

    foreach ($exclusions as $key => $exclusion) {
      $exclusions[$key] = [
        'from' => new \DateTime($exclusion['from']),
        'to' => new \DateTime($exclusion['to']),
      ];
    }

    $start = new \DateTime($origin_dates[0]['from']);
    $end = new \DateTime($origin_dates[0]['to']);

    $resultingPeriods = [];
    $resultingPeriods[] = ['from' => clone $start];

    $prevSkipInstance = FALSE;

    while ($start < $end) {

      // Check if this instance should be excluded.
      $skipInstance = FALSE;
      foreach ($exclusions as $exclusion) {
        if ($start >= $exclusion['from'] && $start <= $exclusion['to']) {
          $skipInstance = TRUE;
          break;
        }
      }

      // We just hit exclusion. Need to close previous resulting period.
      if ($skipInstance && !$prevSkipInstance) {
        $period = array_pop($resultingPeriods);
        $close = clone $start;
        $close->modify('-1 week');
        $close->setTime($end->format('H'), $end->format('i'));
        $period['to'] = $close;
        array_push($resultingPeriods, $period);
      }

      // If exclusion just finished. We need to start new period.
      if (!$skipInstance && $prevSkipInstance) {
        $period = ['from' => clone $start];
        array_push($resultingPeriods, $period);
      }
      $start->modify('+1 week');
      $prevSkipInstance = $skipInstance;
    }

    // Do not forget to close last period.
    $period = array_pop($resultingPeriods);
    $period['to'] = $end;
    array_push($resultingPeriods, $period);

    // Now convert dates back to strings.
    $result_dates = [];
    foreach ($resultingPeriods as $period) {
      // Example format 2018-01-08T05:15:00
      $result_dates[] = [
        'from' => $period['from']->format('Y-m-d\TH:i:s'),
        'to' => $period['to']->format('Y-m-d\TH:i:s'),
      ];
    }

    return $result_dates;
  }
public static function combineDates ( $ origin_dates , $ exclusions ) { if ( empty ( $ exclusions ) || ! is_array ( $ exclusions ) ) { return $ origin_dates ; } foreach ( $ exclusions as $ key => $ exclusion ) { $ exclusions [ $ key ] = [ 'from' => new \ DateTime ( $ exclusion [ 'from' ] ) , 'to' => new \ DateTime ( $ exclusion [ 'to' ] ) , ] ; } $ start = new \ DateTime ( $ origin_dates [ 0 ] [ 'from' ] ) ; $ end = new \ DateTime ( $ origin_dates [ 0 ] [ 'to' ] ) ; $ resultingPeriods = [ ] ; $ resultingPeriods [ ] = [ 'from' => clone $ start ] ; $ prevSkipInstance = FALSE ; while ( $ start < $ end ) { // Check if this instance should be excluded. $ skipInstance = FALSE ; foreach ( $ exclusions as $ exclusion ) { if ( $ start >= $ exclusion [ 'from' ] && $ start <= $ exclusion [ 'to' ] ) { $ skipInstance = TRUE ; break ; } } // We just hit exclusion. Need to close previous resulting period. if ( $ skipInstance && ! $ prevSkipInstance ) { $ period = array_pop ( $ resultingPeriods ) ; $ close = clone $ start ; $ close -> modify ( '-1 week' ) ; $ close -> setTime ( $ end -> format ( 'H' ) , $ end -> format ( 'i' ) ) ; $ period [ 'to' ] = $ close ; array_push ( $ resultingPeriods , $ period ) ; } // If exclusion just finished. We need to start new period. if ( ! $ skipInstance && $ prevSkipInstance ) { $ period = [ 'from' => clone $ start ] ; array_push ( $ resultingPeriods , $ period ) ; } $ start -> modify ( '+1 week' ) ; $ prevSkipInstance = $ skipInstance ; } // Do not forget to close last period. $ period = array_pop ( $ resultingPeriods ) ; $ period [ 'to' ] = $ end ; array_push ( $ resultingPeriods , $ period ) ; // Now convert dates back to strings. $ result_dates = [ ] ; foreach ( $ resultingPeriods as $ period ) { // Example format 2018-01-08T05:15:00 $ result_dates [ ] = [ 'from' => $ period [ 'from' ] -> format ( 'Y-m-d\TH:i:s' ) , 'to' => $ period [ 'to' ] -> format ( 'Y-m-d\TH:i:s' ) , ] ; } return $ result_dates ; }
Return combined dates.

@param array $origin_dates
Start and End of the Session.
@param array $exclusions
Exclusions dates in session.

@return array
Return combined dates .