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

1/** 2 * 查询指定时间范围内的所有日期,月份,季度,年份 3 * 4 * @param $startDate 指定开始时间,Y-m-d格式 5 * @param $endDate 指定结束时间,Y-m-d格式 6 * @param $type 类型,day 天,month 月份,quarter 季度,year 年份 7 * @return array 8 */ 9function getDateByInterval($startDate, $endDate, $type) 10{ 11 if (date('Y-m-d', strtotime($startDate)) != $startDate || date('Y-m-d', strtotime($endDate)) != $endDate) { 12 return '日期格式不正确'; 13 } 14 15 $tempDate = $startDate; 16 $returnData = []; 17 $i = 0; 18 if ($type == 'day') { // 查询所有日期 19 while (strtotime($tempDate) < strtotime($endDate)) { 20 $tempDate = date('Y-m-d', strtotime('+' . $i . ' day', strtotime($startDate))); 21 $returnData[] = $tempDate; 22 $i++; 23 } 24 } elseif ($type == 'month') { // 查询所有月份以及开始结束时间 25 while (strtotime($tempDate) < strtotime($endDate)) { 26 $temp = []; 27 $month = strtotime('first day of +' . $i . ' month', strtotime($startDate)); 28 $temp['name'] = date('Y-m', $month); 29 $temp['startDate'] = date('Y-m-01', $month); 30 $temp['endDate'] = date('Y-m-t', $month); 31 $tempDate = $temp['endDate']; 32 $returnData[] = $temp; 33 $i++; 34 } 35 } elseif ($type == 'quarter') { // 查询所有季度以及开始结束时间 36 while (strtotime($tempDate) < strtotime($endDate)) { 37 $temp = []; 38 $quarter = strtotime('first day of +' . $i . ' month', strtotime($startDate)); 39 $q = ceil(date('n', $quarter) / 3); 40 $temp['name'] = date('Y', $quarter) . '第' . $q . '季度'; 41 $temp['startDate'] = date('Y-m-01', mktime(0, 0, 0, $q * 3 - 3 + 1, 1, date('Y', $quarter))); 42 $temp['endDate'] = date('Y-m-t', mktime(23, 59, 59, $q * 3, 1, date('Y', $quarter))); 43 $tempDate = $temp['endDate']; 44 $returnData[] = $temp; 45 $i = $i + 3; 46 } 47 } elseif ($type == 'year') { // 查询所有年份以及开始结束时间 48 while (strtotime($tempDate) < strtotime($endDate)) { 49 $temp = []; 50 $year = strtotime('+' . $i . ' year', strtotime($startDate)); 51 $temp['name'] = date('Y', $year) . '年'; 52 $temp['startDate'] = date('Y-01-01', $year); 53 $temp['endDate'] = date('Y-12-31', $year); 54 $tempDate = $temp['endDate']; 55 $returnData[] = $temp; 56 $i++; 57 } 58 } 59 return $returnData; 60}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

java常用类(2)

三、时间处理相关类Date类:计算机世界把1970年1月1号定为基准时间,每个度量单位是毫秒(1秒的千分之一),用long类型的变量表示时间。Date分配Date对象并初始化对象,以表示自从标准基准时间(称为“历元”(epoch),即1970年1月1日08:00:00GMT)以来的指定毫秒数。示例:packagecn.tanjian

Python之time模块的时间戳、时间字符串格式化与转换

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y