ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-01 10:03:08  阅读:282  来源: 互联网

标签:startDate 年份 endDate temp tempDate 查询 strtotime date 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)));
            $returnData[] = $tempDate;
            $i++;
        }
    } elseif ($type == 'month') {    // 查询所有月份以及开始结束时间
        while (strtotime($tempDate) < strtotime($endDate)) {
            $temp = [];
            $month = strtotime('+' . $i . ' month', strtotime($startDate));
            $temp['name'] = date('Y-m', $month);
            $temp['startDate'] = date('Y-m-01', $month);
            $temp['endDate'] = date('Y-m-t', $month);
            $tempDate = $temp['endDate'];
            $returnData[] = $temp;
            $i++;
        }
    } elseif ($type == 'quarter') {    // 查询所有季度以及开始结束时间
        while (strtotime($tempDate) < strtotime($endDate)) {
            $temp = [];
            $quarter = strtotime('+' . $i . ' month', strtotime($startDate));
            $q = ceil(date('n', $quarter) / 3);
            $temp['name'] = date('Y', $quarter) . '第' . $q . '季度';
            $temp['startDate'] = date('Y-m-01', mktime(0, 0, 0, $q * 3 - 3 + 1, 1, date('Y', $quarter)));
            $temp['endDate'] = date('Y-m-t', 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', $year);
            $temp['endDate'] = date('Y-12-31', $year);
            $tempDate = $temp['endDate'];
            $returnData[] = $temp;
            $i++;
        }
    }
    return $returnData;
}

 

标签:startDate,年份,endDate,temp,tempDate,查询,strtotime,date,PHP
来源: https://www.cnblogs.com/woods1815/p/10958642.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有