如何从日期数组中分离日期和日期?

我有一个数组,它只有一个月的日期。


$dates =array (

            '2018-10-15',

            '2018-10-16',

            '2018-10-17',

            '2018-10-13',

            '2018-10-19',

            '2018-10-10',

            '2018-10-11',

            '2018-10-12',

            '2018-10-22',

            '2018-10-23',

            '2018-10-29',


        );

和下面的脚本


usort($dates,function($a,$b){

            return strtotime($a) - strtotime($b);

        });

        $consecutive_added_set = [];

        $consecutive_array = [];

        $temp = [];

        $temp[] = date('Y-m-d',strtotime($dates[0]));

        for($i=1;$i<count($dates);++$i){

            if(strtotime($dates[$i]) - strtotime($dates[$i - 1]) === 86400){ // 1 day gap(86400 seconds)

                $temp[] = date('Y-m-d',strtotime($dates[$i]));

                $consecutive_added_set[$dates[$i-1]] = true;

                $consecutive_added_set[$dates[$i]] = true;

            }else{

                if(count($temp) > 1){

                    $consecutive_array[] = $temp;   

                }

                $temp = [];

                $temp[] = date('Y-m-d',strtotime($dates[$i]));

            }

        }

        if(count($temp) > 1){ //  the last consecutiveness match of dates as well(corner case)

            $consecutive_array[] = $temp;

        }

        $conseq[] = []; // reset the array structure 

        $conseq['consecutive'] = $consecutive_array;

        $conseq['consecutive_count'] = count($consecutive_array);

        $conseq['non_consecutive'] = [];

        foreach($dates as $current_date){

            if(!isset($consecutive_added_set[$current_date])){ // skip all dates which were d for consecutiveness

                $conseq['non_consecutive'][] = date('Y-m-d',strtotime($current_date));

            }

        }


我已经尝试了很多。


守着星空守着你
浏览 86回答 2
2回答

长风秋雁

一个快速的解决方法是获取数组中的第一个和最后一个项目。&nbsp; &nbsp; $consecutive_array = array_map(function($e){&nbsp; &nbsp; &nbsp; &nbsp; if(!is_array($e)){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $e;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $last = end($e);&nbsp; &nbsp; &nbsp; &nbsp; return [reset($e), $last];&nbsp; &nbsp; }, $consecutive_array);或者按照评论中的建议使用min() max() 函数。&nbsp; &nbsp; $consecutive_array = array_map(function($e){&nbsp; &nbsp; &nbsp; &nbsp; if(!is_array($e)){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $e;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return [min($e), max($e)];&nbsp; &nbsp; }, $consecutive_array);

郎朗坤

重要提示:不要指望strtodate一天比第二天的精确少 86400 秒strtodate- 根据服务器的区域设置,会有夏令时,这可能会搞砸!在这种情况下,我倾向于比较strtodate('+1 day',$timestampOfDay1)-strtodate($timestampOfDay2)这将包括夏令时。这是我的做法://the dates$dates =array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-15',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-16',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-17',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-13',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-19',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-10',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-11',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-12',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-22',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-23',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2018-10-29',&nbsp; &nbsp; &nbsp; &nbsp; );//call the function$result = getConsecutive($dates);//outputvar_dump($result);function getConsecutive($dates) {&nbsp; &nbsp; sort($dates);&nbsp; &nbsp; $result = [&nbsp; &nbsp; &nbsp; &nbsp; 'consecutive' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'consecutive_count'=>0,&nbsp; &nbsp; &nbsp; &nbsp; 'non_consecutive' => []&nbsp; &nbsp; ];&nbsp; &nbsp; $currentStart = null;&nbsp; &nbsp; $currentTimestamp = null;&nbsp; &nbsp; for ($i=0; $i<count($dates); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $timestamp = strtotime($dates[$i]);&nbsp; &nbsp; &nbsp; &nbsp; if ($currentStart == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //first timestamp - set it as start & current&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentStart = $timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentTimestamp = $timestamp;&nbsp; &nbsp; &nbsp; &nbsp; } else if (strtotime('+1 day',$currentTimestamp) == $timestamp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //consecutive - keep waiting for a non-consecutive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentTimestamp = $timestamp;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($currentTimestamp == $currentStart) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //we just got one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //we just got more then one, so they were consecutive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result['consecutive']['dates'.(count($result['consecutive'])+1)] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date('Y-m-d',$currentStart),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date('Y-m-d',$currentTimestamp)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentStart = $timestamp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentTimestamp = $timestamp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //process the last timestamp&nbsp; &nbsp; if ($currentTimestamp == $currentStart) {&nbsp; &nbsp; &nbsp; &nbsp; //we just got one&nbsp; &nbsp; &nbsp; &nbsp; $result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; //we just got more then one, so they were consecutive&nbsp; &nbsp; &nbsp; &nbsp; $result['consecutive']['dates'.(count($result['consecutive'])+1)] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date('Y-m-d',$currentStart),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date('Y-m-d',$currentTimestamp)&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }&nbsp; &nbsp; $result['consecutive_count'] = count($result['consecutive']);&nbsp; &nbsp; return $result;}
打开App,查看更多内容
随时随地看视频慕课网APP