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. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  2. 从事IT, 中国IT人员最值得骄傲的时候

    大学的专业是学习经济与贸易的,后来接触了IT产业,于是乎自己对IT产业的经济王国就特别感兴趣,经济和IT 就像自己的老婆情人一样.令人着迷不舍. IT热和互联网热现在相信人尽皆知.我想告诉那些即将成为 ...

  3. 删除heroku上的数据库记录

    部署本地项目到heroku上.在线上插入数据到数据库,本地代码再次更新到heroku,线上的数据记录还存在单是图片丢失.问题还没有解决: 本地代码和heroku代码怎样同步? heroku使用的pg和 ...

  4. thinkphp5的Illegal string offset 'id'错误

    thinkphp5的Illegal string offset 'id'错误 问题 解答 数组同名了,一个html页面传进来两个cateres的数组,所以在找id的时候不知道找这两个里面的哪一个 第一 ...

  5. MDNS DDoS 反射放大攻击——攻击者假冒被攻击者IP向网络发送DNS请求,域名为“_services._dns-sd._udp.local”,这将引起本地网络中所有提供服务的主机都向被攻击者IP发送DNS响应,列举网络中所有服务

    MDNS Reflection DDoS 2015年3月,有报告叙述了mDNS 成为反射式和放大式 DDoS 攻击中所用媒介的可能性,并详述了 mDNS 反射式攻击的原理和相应防御方式.Q3,Akam ...

  6. rest_framework 分页三种

    .分页 a. 分页 看第n页 每页显示n条数据: b. 分页 在某个位置 向后查看多少条数据 c. 加密分页 上一页和下一页 本质:查看 记住页码id的最大值和最小值 通过其来准确扫描 过去的话 会从 ...

  7. Square roots

    Loops are often used in programs that compute numerical results by starting with an approximate answ ...

  8. Intellij使用"easyexplore"

    刚开始接触Intellij,里面有很多东西还不太会用,平时在eclipse里面用的很方便的easyexplore能帮助快速打开文件目录,Intellij中本身就有这样的功能,只是默认没有开启,需要我们 ...

  9. Remmina:一个 Linux 下功能丰富的远程桌面共享工具(转载)

    Remmina:一个 Linux 下功能丰富的远程桌面共享工具 作者: Aaron Kili 译者: LCTT geekpi | 2017-05-10 09:05   评论: 2 收藏: 4 Remm ...

  10. php文件加载、错误处理、方法函数和数组

    数组运算符注意:php中,数组的元素的顺序,不是由下标(键名)决定的,而是完全由加入的顺序来决定.联合(+):将右边的数组项合并到左边数组的后面,得到一个新数组.如有重复键,则结果以左边的为准$v1 ...