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('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中查询指定时间范围内的所有日期,月份,季度,年份的更多相关文章
- SQL Server中获取指定时间段内的所有日期
DECLARE @days INT, @date_start DATETIME = '2016-11-01', @date_end DATETIME = '2016-11-10' SET @days ...
- Oracle查询指定某一天数据,日期匹配
在做一个功能的时候,需要在oracle数据库中查询指定某一天的数据. 如果是简单的当前日期前后几天,也好办 AND TO_CHAR(Rct.Creation_Date, 'YYYY-MM-DD')=t ...
- Java产生指定范围内的随机日期
要想产生指定范围内的随机日期,首先我们要指定一个范围,那么我们可以通过SImpleDateFormat格式化日期,然后再通过parse()方法设置日期,返回一个Date类型的日期对象,再转化为时间戳( ...
- 巧用DISPLAY_AWR函数与dba_hist_sqlstat结合查询SQL语句在指定节点指定时间范围内的历史执行计划
1.问题 通过调用dbms_xplan包中DISPLAY_AWR函数(DBMS_XPLAN.DISPLAY_AWR)可以从AWR数据中查看到SQL语句的历史执行计划,但是,DISPLAY ...
- AM二次开发中选择指定范围内的对象
使用Spatial可以快速选择指定范围内的对象 例如下面的代码可以选择所有在[0,0,0]-[10m,10m,10m]这个盒子之内的对象: 其中ElementsInBox还可以指定对象类型做进一步筛选 ...
- SQL Server中获取指定时间段内的所有月份
例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...
- ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录
在datagrid中toolbar添加searchbox查询框,根据列范围查询数据,先看效果图: 1. searchbox采用easyui的Demo例子,再加以js扩展,根据datagrid中的列数据 ...
- 数据库SQL语句查询指定时间段内的数据
[摘要]有的时候,我们需要查询数据库某段时间之间的数据,比如2016年5月1号到到5月3号之间用户注册数量(特殊节假日期间)等.那么用SQL语句如何实现呢? 首先,数据表中的存时间的字段比如是addt ...
- MongoDB 用MongoTemplate查询指定时间范围的数据
mongoDB大于小于符号对应: > 大于 $gt< 小于 $lt>= 大于等于 $gte<= 小于等于 $lte 要查询同一个时间多个约束可能出现的error: org.sp ...
随机推荐
- VMware安装vmtools实现宿主机和虚拟机共享粘贴板
打开VMware以Ubuntu14.04.6为例,保持网络畅通,在线下载工具包 下载完成后dvd下出现该压缩包 将其复制到桌面并在桌面进入终端 执行命令: tar -zxvf xxxx.tar.gz ...
- LeetCode刷题191121
博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 数据库: 编写一个 SQL 查询,来删除 Person 表中所有重复的电 ...
- Java_枚举Enum基本使用
特性 在某些情况下,一个类的对象时有限且固定的,如季节类,它只有春夏秋冬4个对象这种实例有限且固定的类,在 Java 中被称为枚举类: 在 Java 中使用 enum 关键字来定义枚举类,其地位与 c ...
- 重启宝塔面板后提示-ModuleNotFoundError: No module named 'geventwebsocket'
背景: 因服务器部署了flask项目,安装了python3,故重启宝塔面板报错 [Traceback (most recent call last): File , in load_class mod ...
- Violet 6 杯省选模拟赛 蒲公英
https://www.luogu.com.cn/problem/P4168 题目 给$n$个数字,有$m$次询问,问$a_l, a_{l+1} , \dots , a_r$的众数是什么, $1\le ...
- RabbitMQ 匿名队列断开问题定位记录
RabbitMQ 匿名队列断开问题定位分析 1 问题现象 平台中,服务的信息交互通过RabbitMQ进行.在实际的使用中,发现系统启动后,就会出现status 监控的mq connection断 ...
- Mybatis1
一.MyBatis介绍 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis ...
- ThinkPHP中框架查询
1.查询多条数据 1.1静态调用all方法或者select方法 // 获取所有数据 all 和 select $list = User::all(); // 根据主键获取多个数据al ...
- Add an Item to the New Action 在新建按钮中增加一个条目
In this lesson, you will learn how to add an item to the New Action (NewObjectViewController.NewObje ...
- 剑指offer笔记面试题12----矩阵中的路径
题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格.如果一条路径经过了矩阵的某一格,那么该路径 ...