Query all dates within the specified time range (hour, day, month, quarter, year)

core method
	/**
     * @name: 查询指定时间范围内的所有日期[支持类型:小时、天、月份、季度、年份]
     * @param {string} $startDate   指定开始时间格式:Y-m-d H:i:s
     * @param {string} $endDate     指定结束时间格式:Y-m-d H:i:s
     * @param {string} $type        类型:hour 小时;day 天;month 月份;quarter 季度;year 年份
     * @param {int} $interval       时间间隔[默认为1,非特殊情况下基本无须传参]
     * @author: Turbo
     * @Date: 2022-02-14 10:23:01
     */    
    public function dateByInterval(string $startDate, string $endDate, string $type = '', int $interval = 1)
    {
   
    
    
        if (
            date('Y-m-d H:i:s', strtotime($startDate)) != $startDate || 
            date('Y-m-d H:i:s', strtotime($endDate)) != $endDate
        ) {
   
    
    
            return '日期格式不正确';
        }

        if(empty($type)){
   
    
    
            return 'type参数缺乏';
        }
    
        $returnData = [];
        $i = 0;

        // 查询时间范围内所有小时
        if ($type == 'hour') {
   
    
    
            $tempDate = date('Y-m-d H'.':00:00', strtotime($startDate));
            $endDate = date('Y-m-d H'.':00:00', strtotime("- {
     
      
      $interval} hour +1 hour", strtotime($endDate)));
            while (strtotime($tempDate) < strtotime($endDate)) {
   
    
    
                $tempDate = date('Y-m-d H'

Guess you like

Origin blog.csdn.net/qq_15957557/article/details/123127554