Search code examples
phpdatetimeperiod

Days Array to Periods


I have an array of days that is similar to the following:

array(10) {
 [0]=>
  string(8) "01/06/18"
 [1]=>
  string(8) "02/06/18"
 [2]=>
  string(8) "03/06/18"
 [3]=>
  string(8) "11/06/18"
 [4]=>
  string(8) "12/06/18"
 [5]=>
   string(8) "13/06/18"
 [6]=>
   string(8) "14/06/18"
 [7]=>
   string(8) "15/06/18"
 [8]=>
   string(8) "16/06/18"
 [9]=>
   string(8) "20/06/18"
}

I am trying solutions so that from the array of days i get the periods in the following manner:

Period 1:
    01/06/18 - 03/06/18
Period 2:
    11/06/18 - 16/06/18
Period 3:
    20/06/18 - 20/06/18

What is the shortest possible way!?

This is what i am trying but with no success:

Note: I am changing the formats of the dates to comply with Y-m-d

    private function SetPeriods($days)
{
    // check if input is empty
    if(count($days) == 0) return array();

    // output to fill
    $ranges = array();

    // order dates keys in order to have the first date in temporary order
    ksort($days);

    // get first and last day
    $firstday = key($days);
    end($days);
    $lastday = key($days);

    // get the type of first day (actually the current day where we looks)
    $current_type = $days[$firstday];

    // using datetime object for easy step of 1 day
    $datetime = new DateTime($days[$firstday]);
    $datetime->setTime(9,0,0); // avoid time problems at midnight (it's needed?)

    // do the first step outside the while
    $datetime->add(new DateInterval('P1D'));

    // store old value of day
    $oldday = $firstday;

    // build the first range
    $ranges[] = array($firstday,null,$current_type);

    while(($day = $datetime->format('Y-m-d')) <= $lastday) {

        // if there are holes, fill it with null
        if(!isset($days[$day])) {
            $days[$day] = null;
        }

        // check if type has changed (=>need new range)
        if(($days[$day] !== $current_type)) {
            $ranges[count($ranges)-1][1] = $oldday;
            $ranges[] = array($day,null,$days[$day]);
            $current_type = $days[$day];
        }

        // store previous day
        $oldday = $day;
        // next day
        $datetime->add(new DateInterval('P1D'));

    }

    // complete the last range
    $ranges[count($ranges)-1][1] = $lastday;

    // remove range of holes
    foreach($ranges as $k=>$range) {
        if(is_null($range[2])) {
            unset($ranges[$k]);
        }
    }

    return $ranges;
}

The problem is now i get only one date i don't know what is the problem cant figure it out


Solution

  • Here is one method, not sure if it's the shortest.

    I convert to Unix time and if the Unix time compared to previous Unix time is more than one day I create a new subarray to store it in.

    Then the res array will hold the periods in Unix times.
    I loop over it and convert to dates grabbing min and Max values only.

    $i =-1;
    $prev =0;
    $format = "d/m/y";
    
    Foreach($arr as $val){
        $dt = date_create_from_format ($format , $val);
        $unix = date_timestamp_get($dt);
    
        If($unix -$prev > 86400) $i++;
        $res[$i][] = $unix;
        $prev = $unix;
    }
    
    Foreach($res as $period){
        If(count($period) >1){
            $periods[] = date($format, min($period)) . " - " . date($format, max($period));
        }Else{
            $periods[] = date($format, min($period));
        }
    }
    Var_dump($periods);
    

    https://3v4l.org/gHDC0



    Now that I think of it, it can be made slightly shorter.
    I skip the last foreach because I can do it on the fly in the main loop except the last item that is done after the loop.

    https://3v4l.org/buaCN

    $i =-1;
    $prev =0;
    $format = "d/m/y";
    
    Foreach($arr as $val){
        $dt = date_create_from_format ($format , $val);
        $unix = date_timestamp_get($dt);
    
        If($unix -$prev > 86400){
            $i++;
            If($i>0){
                If(count($res[$i-1]) >1){
                    $periods[] = date($format, min($res[$i-1])) . " - " . date($format, max($res[$i-1]));
                }Else{
                    $periods[] = date($format, min($res[$i-1]));
                }
            }
        }
        $res[$i][] = $unix;
        $prev = $unix;
    }
    
    If(count(end($res)) >1){
        $periods[] = date($format, min(end($res))) . " - " . date($format, max(end($res)));
    }Else{
        $periods[] = date($format, min(end($res)));
    }
    
    Var_dump($periods);