第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從日期數(shù)組中分離日期和日期?

如何從日期數(shù)組中分離日期和日期?

PHP
守著星空守著你 2022-07-16 16:27:02
我有一個(gè)數(shù)組,它只有一個(gè)月的日期。$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));            }        }我已經(jīng)嘗試了很多。
查看完整描述

2 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

一個(gè)快速的解決方法是獲取數(shù)組中的第一個(gè)和最后一個(gè)項(xiàng)目。


    $consecutive_array = array_map(function($e){

        if(!is_array($e)){ 

            return $e;

        }

        $last = end($e);

        return [reset($e), $last];

    }, $consecutive_array);

或者按照評(píng)論中的建議使用min() max() 函數(shù)。


    $consecutive_array = array_map(function($e){

        if(!is_array($e)){ 

            return $e;

        }

        return [min($e), max($e)];

    }, $consecutive_array);


查看完整回答
反對(duì) 回復(fù) 2022-07-16
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

重要提示:不要指望strtodate一天比第二天的精確少 86400 秒strtodate- 根據(jù)服務(wù)器的區(qū)域設(shè)置,會(huì)有夏令時(shí),這可能會(huì)搞砸!


在這種情況下,我傾向于比較strtodate('+1 day',$timestampOfDay1)-strtodate($timestampOfDay2)這將包括夏令時(shí)。


這是我的做法:


//the dates

$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',


        );


//call the function

$result = getConsecutive($dates);


//output

var_dump($result);


function getConsecutive($dates) {

    sort($dates);


    $result = [

        'consecutive' => [],

        'consecutive_count'=>0,

        'non_consecutive' => []

    ];


    $currentStart = null;

    $currentTimestamp = null;


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

        $timestamp = strtotime($dates[$i]);


        if ($currentStart == null) {

            //first timestamp - set it as start & current

            $currentStart = $timestamp;

            $currentTimestamp = $timestamp;

        } else if (strtotime('+1 day',$currentTimestamp) == $timestamp) {

            //consecutive - keep waiting for a non-consecutive

            $currentTimestamp = $timestamp;

        } else {

            if ($currentTimestamp == $currentStart) {

                //we just got one

                $result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);

            } else {

                //we just got more then one, so they were consecutive

                $result['consecutive']['dates'.(count($result['consecutive'])+1)] = [

                    date('Y-m-d',$currentStart),

                    date('Y-m-d',$currentTimestamp)

                ];

            }

            $currentStart = $timestamp;

            $currentTimestamp = $timestamp;

        }

    }

    //process the last timestamp

    if ($currentTimestamp == $currentStart) {

        //we just got one

        $result['non_consecutive'][] = date('Y-m-d',$currentTimestamp);

    } else {

        //we just got more then one, so they were consecutive

        $result['consecutive']['dates'.(count($result['consecutive'])+1)] = [

            date('Y-m-d',$currentStart),

            date('Y-m-d',$currentTimestamp)

        ];

    }

    $result['consecutive_count'] = count($result['consecutive']);


    return $result;

}



查看完整回答
反對(duì) 回復(fù) 2022-07-16
  • 2 回答
  • 0 關(guān)注
  • 115 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)