date()函数:

  PHP date()  参考:https://www.hi-docs.com/php/date.html

  Linux date()参考:http://www.cnblogs.com/barrychiao/archive/2012/10/08/2715808.html

获取某时间段以周、月、季度为粒度的时间段数组

 function getdays($day){//指定天的周一和周天
$lastday=date('Y-m-d',strtotime("$day Sunday"));
$firstday=date('Y-m-d',strtotime("$lastday -6 days"));
return array($firstday,$lastday);
}
function getmonths($day){//指定月的第一天和最后一天
$firstday = date('Y-m-01',strtotime($day));
$lastday = date('Y-m-d',strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday); }
/**
* 输入开始时间,结束时间,粒度(周,月,季度)
* @param 参数一:开始时间
* @param 参数二:结束时间
* @param 参数三:粒度(周,月,季度)
* @return 时间段字符串数组
*/
function get_ld_times($st,$et,$ld){
if($ld=='周'){
$timeArr=array();
$t1=$st;
$t2=getdays($t1)['1'];
while($t2<$et || $t1<=$et){//周为粒度的时间数组
$timeArr[]=$t1.','.$t2;
$t1=date('Y-m-d',strtotime("$t2 +1 day"));
$t2=getdays($t1)['1'];
$t2=$t2>$et?$et:$t2;
}
return $timeArr;
}else if($ld=='月'){
$timeArr=array();
$t1=$st;
$t2=getmonths($t1)['1'];
while($t2<$et || $t1<=$et){//月为粒度的时间数组
$timeArr[]=$t1.','.$t2;
$t1=date('Y-m-d',strtotime("$t2 +1 day"));
$t2=getmonths($t1)['1'];
$t2=$t2>$et?$et:$t2;
}
return $timeArr;
}else if($ld=='季度'){
$tStr=explode('-',$st);
$month=$tStr['1'];
if($month<=3){
$t2=date("$tStr[0]-03-31");
}else if($month<=6){
$t2=date("$tStr[0]-06-30");
}else if($month<=9){
$t2=date("$tStr[0]-09-30");
}else{
$t2=date("$tStr[0]-12-31");
}
$t1=$st;
$t2=$t2>$et?$et:$t2;
$timeArr=array();
while($t2<$et || $t1<=$et){//月为粒度的时间数组
$timeArr[]=$t1.','.$t2;
$t1=date('Y-m-d',strtotime("$t2 +1 day"));
$t2=date('Y-m-d',strtotime("$t1 +3 months -1 day"));
$t2=$t2>$et?$et:$t2;
}
return $timeArr;
}else{
return array('参数错误!');
}
}

PHP date()获取某时间段以周、月、季度为粒度的时间段数组的更多相关文章

  1. java获取当前时间的年周月季度等的开始结束时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  2. java获取当前年、半年、季度、月、日、小时 开始结束时间等

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 获取 当前年.半年 ...

  3. C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...

  4. mysql中增加某一时间段内的时间数据(包含:时间、年、月、日、第几周、季度)

    创建表dim_date: create table `dim_date` ( `year` int (20), `month` int (20), `day` int (20), `week` int ...

  5. Flex自定义组件开发之日周月日期选择日历控件

    原文:Flex自定义组件开发之日周月日期选择日历控件         使用过DateField的我们都知道,DateField 控件是用于显示日期的文本字段,字段右侧带有日历图标.当用户在控件边框内的 ...

  6. [转] Js获取 本周、本月、本季度、本年、上月、上周、上季度、去年时间段

    /** * 针对时间的工具类 */ var DateTimeUtil = function () { /*** * 获得当前时间 */ this.getCurrentDate = function ( ...

  7. js 获取每月有几周,根据年月周获取该周从周一到周日的日期等方法

    本文基于react-native 本人在用react-native写一个关于课程表的APP时需要课程表按照日期周期显示,网上查了许多方法,都没有达到自己想要的效果,根据一些方法的参考,再根据自己思维写 ...

  8. PHP获取接下来一周的日期

    //获取接下来一周的日期 function GetWeeks() { $i=0; $weeks=[]; for ($i;$i<=7;$i++){ $month=date('m',time()+8 ...

  9. PHP获取某年第几周的开始日期和结束日期

    http://blog.csdn.net/qq_27080247/article/details/50835956 /** * 获取某年第几周的开始日期和结束日期 * @param int $year ...

随机推荐

  1. 【codeforces 335A】Banana

    [题目链接]:http://codeforces.com/contest/335/problem/A [题意] 让你构造一个长度为n的字符串; 每次你可以从这个字符串中任意取走字符; 让你求出取的次数 ...

  2. linux的一页是多大

    命令 getconf PAGESIZE 结果为4096,即一页=4096字节=4KB(注意是Byte,1B=8bit) 在使用mmap映射函数时,它的实际映射单位也是以页为单位的,即不过我们把MAP_ ...

  3. 在swt中获取jar包中的文件 uri is not hierarchical

    uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...

  4. iOS gzip解压

    1. 导入libz库(如:libz 1.2.5.dylib) 2. 引入头文件 #import "zlib.h" 3. 实现解压(输入输出都为NSData对象) -(NSData ...

  5. mvc中使用remote属性来做ajax验证

    mvc中使用remote属性来做ajax验证比較easy : [Remote("Action", "Controller", AdditionalFields ...

  6. 数据结构之fhq-treap

    本文内容部分转自某大佬博客:https://blog.csdn.net/CABI_ZGX/article/details/79963427 例题:https://www.luogu.org/probl ...

  7. Lists and tuples

    zip is a built-in function that takes two or more sequence and ‘zips’ them into a list of tuples, wh ...

  8. POJ 3013 Dijkstra

    从1节点最短路,,然后再乘一下权值就OK了 //By SiriusRen #include <queue> #include <cstdio> #include <cst ...

  9. python 3.x 写 shell sed 替换功能

    shell sed 替换功能 f1 = open('yesterday','r',encoding='utf-8') f2 = open('yesterday2','w',encoding='utf- ...

  10. NodeJS学习笔记 进阶 (7)express+session实现简易身份认证(ok)

    个人总结: 这篇文章讲解了express框架中如何使用session,主要用了express-session这个包.更多可以参考npm.js来看,读完这篇文章需要10分钟. 摘选自网络: 文档概览 本 ...