PHP根据相差的天数获取连续的时间段

php

浏览数:300

2019-1-8

/**
     * 根据相差的天数获取所有连续的时间段
     * @param $diffDay
     * @param string $dateFormat
     * @return array
     */
    public static function getContinuesDayDiffDay($diffDay, $dateFormat = 'Y-m-d') {
        $today = date('Y-m-d');
        $timeLabel = [];
        for ($i=1;$i<=$diffDay;$i++){
            $diff = $diffDay - $i;
            $mday = date($dateFormat,strtotime("$today -$diff day"));
            array_push($timeLabel,$mday);
        }
        //转化查询条件
        $year = date('Y');
        $startDay = str_replace('.','-',$timeLabel[0]);
        $endDay = str_replace('.','-',$timeLabel[$diffDay-1]);
        $startTime = strtotime($startDay." 00:00:00");
        $endTime = strtotime($endDay." 23:59:59");
        return [
            'start_time' => $startTime,
            'end_time' => $endTime,
            'time_label' => $timeLabel,
        ];
    }