PHP 查询指定时间范围内的所有日期,月份,季度,年份

   /**
     * 查询指定时间范围内的所有日期,月份,季度,年份
     * @param $startDate   指定开始时间,Y-m-d格式
     * @param $endDate     指定结束时间,Y-m-d格式
     * @param $type        类型,day 天,month 月份,quarter 季度,year 年份
     * @return array
     */
    function getDateByInterval($startDate, $endDate, $type)
    {
        if (date('Y-m-d', strtotime($startDate)) != $startDate || date('Y-m-d', strtotime($endDate)) != $endDate) {
            return '日期格式不正确';
        }
    
        $tempDate = $startDate;
        $returnData = [];
        $i = 0;
        if ($type == 'day') {    // 查询所有日期
            while (strtotime($tempDate) < strtotime($endDate)) {
                $tempDate = date('Y-m-d', strtotime('+' . $i . ' day', strtotime($startDate)));
                $temp = [];
                $temp['name'] = $tempDate;
                $temp['startDate'] = date("Y-m-d 00:00:00", strtotime($tempDate));
                $temp['endDate'] = date("Y-m-d 23:59:59", strtotime($tempDate));
                $returnData[] = $temp;
                $i++;
            }
        } elseif($type == 'week'){
            $startdate = strtotime($startDate);
            $enddate = strtotime($endDate);
            if($startdate <= $enddate){
                $end_date = strtotime("next monday", $enddate);
                if(date("w", $startdate)==1){
                    $start_date = $startdate;
                }else{
                    $start_date = strtotime("last monday", $startdate);
                }
                //计算时间差多少周
                $countweek = ($end_date-$start_date)/(7*24*3600);
                for($j = 0; $j < $countweek; $j++){
                    $ed = strtotime("+ 6 days",$start_date);
                    $start_date = strtotime("+ 1 day", $ed);
                    $temp = [];
                    $temp['name'] = date('W', $ed);
                    $temp['startDate'] = date("Y-m-d 00:00:00", $start_date);
                    $temp['endDate'] = date("Y-m-d 23:59:59", $ed);
                    $returnData[] = $temp;
                }
            }
        } elseif ($type == 'month') {    // 查询所有月份以及开始结束时间
            while (strtotime($tempDate) < strtotime($endDate)) {
                $temp = [];
                $month = strtotime('first day of +' . $i . ' month', strtotime($startDate));
                $temp['name'] = date('Y-m', $month);
                $temp['startDate'] = date('Y-m-01 00:00:00', $month);
                $temp['endDate'] = date('Y-m-t 23:59:59', $month);
                $tempDate = $temp['endDate'];
                $returnData[] = $temp;
                $i++;
            }
        } elseif ($type == 'quarter') {    // 查询所有季度以及开始结束时间
            while (strtotime($tempDate) < strtotime($endDate)) {
                $temp = [];
                $quarter = strtotime('first day of +' . $i . ' month', strtotime($startDate));
                $q = ceil(date('n', $quarter) / 3);
                $temp['name'] = date('Y', $quarter) . '第' . $q . '季度';
                $temp['startDate'] = date('Y-m-01 00:00:00', mktime(0, 0, 0, $q * 3 - 3 + 1, 1, date('Y', $quarter)));
                $temp['endDate'] = date('Y-m-t 23:59:59', mktime(23, 59, 59, $q * 3, 1, date('Y', $quarter)));
                $tempDate = $temp['endDate'];
                $returnData[] = $temp;
                $i = $i + 3;
            }
        } elseif ($type == 'year') {    // 查询所有年份以及开始结束时间
            while (strtotime($tempDate) < strtotime($endDate)) {
                $temp = [];
                $year = strtotime('+' . $i . ' year', strtotime($startDate));
                $temp['name'] = date('Y', $year) . '年';
                $temp['startDate'] = date('Y-01-01 00:00:00', $year);
                $temp['endDate'] = date('Y-12-31 23:59:59', $year);
                $tempDate = $temp['endDate'];
                $returnData[] = $temp;
                $i++;
            }
        }
        return $returnData;
    }

One thought on “PHP 查询指定时间范围内的所有日期,月份,季度,年份

  1. Hi my family member! I want to say that this post is awesome, nice written and come with approximately all significant infos. I would like to peer extra posts like this.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Back To Top

鄂ICP备17008157号-1