Laravel, PHP split all possible date ranges from a set of dates by consecutive date order


Image title



One of my follower send me this question in email, seeking my help to split a date array in to possible date ranges in PHP Laravel framework, and he want to get ranges based on consecutive day orders.

He has an array which contains a set of dates as in the example bellow.

 $days =array (
    '2018-10-15',
    '2018-10-16',
    '2018-10-17',
    '2018-10-18',
    '2018-10-19',
    '2018-10-20',
    '2018-10-21',
    '2018-10-22',
    '2018-10-23',
    /* MISSING 2018-10-24 */
    '2018-10-25',
    '2018-10-26',
    '2018-10-27',
    '2018-10-28',
    '2018-10-29',
    '2018-10-30',
    '2018-10-31',
    '2018-11-01',
    '2018-11-02',
    /* MISSING 2018-11-03 */
    '2018-11-04',
    '2018-11-05',
    '2018-11-06',
    '2018-11-07',
    '2018-11-08',
    '2018-11-09',
    '2018-11-10',
    '2018-11-11',
    '2018-11-12',
    '2018-11-13',
    '2018-11-14',
    '2018-11-15',
    '2018-11-16',
    '2018-11-17',
    '2018-11-18',
    '2018-11-19',
    '2018-11-20',
    '2018-11-21',
    '2018-11-22',
    '2018-11-23',
    '2018-11-24',
    '2018-11-25',
    '2018-11-26',
    '2018-11-27',
    '2018-11-28',
    '2018-11-29',
    '2018-11-30',
    '2018-12-01',
    '2018-12-02',
    '2018-12-03',
    '2018-12-04',
    '2018-12-05',
    '2018-12-06',
    '2018-12-07',
    '2018-12-08',
    '2018-12-09',
    '2018-12-10',
);

 before jump to write our solution, let looks at how our expected output should be for above set of dates,

$date_ranges = Array ( 
       [0] => Array ( [0] => 2018-10-15 [1] => 2018-10-23 ) 
       [1] => Array ( [0] => 2018-10-25 [1] => 2018-11-02 ) 
       [2] => Array ( [0] => 2018-11-04 [1] => 2018-12-10 ) 
 ) 

Looking at the expected final out put,
we have 3 possible ranges from the set of dates ,  because "2018-10-24", "2018-11-03" are missing in the dates array, which breaks the consecutive order.

Here is the PHP function which I wrote as a helper function which expect date array argument and can bring the output as in the example above,

function getDateRangeSplitted($dates)
{
    sort($dates);
    $startDate  = $dates[0];
    $finishDate = array_pop($dates);
    // walk through the dates, breaking at gaps
    foreach ($dates as $key => $date)
        if (($key > 0) && (strtotime($date)-strtotime($dates[$key-1]) > 99999)) {
            $result[] = array($startDate,$dates[$key-1]);
            $startDate = $date;
        }
    // force the end
    $result[] = array($startDate,$finishDate);
    return $result;
}



Written by Akram Wahid 5 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}