/**
* 查询指定时间范围内的所有日期,月份,季度,年份
*
* @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('first day of +' . $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('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', 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;
}

PHP中查询指定时间范围内的所有日期,月份,季度,年份的更多相关文章

  1. SQL Server中获取指定时间段内的所有日期

    DECLARE @days INT, @date_start DATETIME = '2016-11-01', @date_end DATETIME = '2016-11-10' SET @days ...

  2. Oracle查询指定某一天数据,日期匹配

    在做一个功能的时候,需要在oracle数据库中查询指定某一天的数据. 如果是简单的当前日期前后几天,也好办 AND TO_CHAR(Rct.Creation_Date, 'YYYY-MM-DD')=t ...

  3. Java产生指定范围内的随机日期

    要想产生指定范围内的随机日期,首先我们要指定一个范围,那么我们可以通过SImpleDateFormat格式化日期,然后再通过parse()方法设置日期,返回一个Date类型的日期对象,再转化为时间戳( ...

  4. 巧用DISPLAY_AWR函数与dba_hist_sqlstat结合查询SQL语句在指定节点指定时间范围内的历史执行计划

    1.问题        通过调用dbms_xplan包中DISPLAY_AWR函数(DBMS_XPLAN.DISPLAY_AWR)可以从AWR数据中查看到SQL语句的历史执行计划,但是,DISPLAY ...

  5. AM二次开发中选择指定范围内的对象

    使用Spatial可以快速选择指定范围内的对象 例如下面的代码可以选择所有在[0,0,0]-[10m,10m,10m]这个盒子之内的对象: 其中ElementsInBox还可以指定对象类型做进一步筛选 ...

  6. SQL Server中获取指定时间段内的所有月份

    例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...

  7. ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录

    在datagrid中toolbar添加searchbox查询框,根据列范围查询数据,先看效果图: 1. searchbox采用easyui的Demo例子,再加以js扩展,根据datagrid中的列数据 ...

  8. 数据库SQL语句查询指定时间段内的数据

    [摘要]有的时候,我们需要查询数据库某段时间之间的数据,比如2016年5月1号到到5月3号之间用户注册数量(特殊节假日期间)等.那么用SQL语句如何实现呢? 首先,数据表中的存时间的字段比如是addt ...

  9. MongoDB 用MongoTemplate查询指定时间范围的数据

    mongoDB大于小于符号对应: > 大于 $gt< 小于 $lt>= 大于等于 $gte<= 小于等于 $lte 要查询同一个时间多个约束可能出现的error: org.sp ...

随机推荐

  1. Vue-Cli 3.0 中配置高德地图

    vue 中使用高德地图有两种方式 一.vue-amap 组件 官网: https://elemefe.github.io/vue-amap/#/ 开始的时候是打算用这个组件做地图功能的,但是尝试之后存 ...

  2. zhy2_rehat6_mysql04 - MHA_故障演示与切换.txt

    export LANG=en_US 环境:------------------------------------------ 机器 VPN ip linux 账号/密码manager1 172.28 ...

  3. centos7.6 安装Tomcat-8.5.39

    #关闭防火墙 systemctl stop firewalld.service systemctl disable firewalld setenforce sed -i '/SELINUX=/ s/ ...

  4. Codeforces 7E - Defining Macros 题解

    目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写 ...

  5. [洛谷P1122][题解]最大子树和

    这是一道还算简单的树型dp. 转移方程:f[i]=max(f[j],0) 其中i为任意非叶节点,j为i的一棵子树,而每棵子树都有选或不选两种选择 具体看代码: #include<bits/std ...

  6. 删除文件linux

    bool LxDeleteFile(const char* src){ int32_t iRe = remove(src); ) return true; else return false; }

  7. numpy函数查询手册

    写了个程序,对Numpy的绝大部分函数及其说明进行了中文翻译. 原网址:https://docs.scipy.org/doc/numpy/reference/routines.html#routine ...

  8. dev c++必须修改的三处默认设置

    此文档记录参加pat考试并且以dev c++[针对5.11版本]软件作为开发工具时,必须修改的三个默认设置. 1.修改默认语言标准 Dev C++ 5.11 版本支持较新的 C 和 C++ 标准,但是 ...

  9. 在IntelliJ IDEA中,Lombok注解@Slf4j找不到log解决方案

    在IntelliJ IDEA中,注解@Slf4j找不到log时,可以安装Lombok插件 File → settings → Plugins, 然后点击"Browse repositorie ...

  10. vue的provide和inject特性

    由来 组件之间的通信可以通过props和$emit的方式进行通信,但是如果组件之间的关系非常复杂的话,通过以上的方式会很麻烦,并且程序会非常脆弱,没有建中性可言. 在==vue2.2.0 中新增pro ...