企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
获得时间区间 @param integer $time 2019-2-26  @param integer $intervalType 区间类型 1:上周,2:本月,3:上一个月,4:本季度,5:上一个季度,6:本年,7:去年,8:昨天 @detail 比如:1.本周是周一到周二  同比本周:上周的周一到周二 2.本月是1-5号  同比本月:上个月的1-5号  @author wangjun11@leju.com @return array('stime' => 2019-02-26, 'etime' =>  2019-02-28 23:59:59) ```  public function getIntervalTime($time, $intervalType = 1)     {        switch ($intervalType)        {            case 2:               // 本月               $stime = date('Y-m-d', strtotime(date('Y-m', $time)));               $etime = date('Y-m-d', $time)." 23:59:59";               break;            case 3:               // 同比本月(上个月)               $stime = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', $time))));                // 上月有28天 查的是31号的数据               $yday = date('t', strtotime($stime));; // 上个月的天数               $byday = date('t', $time); // 本月的天数               $bday = date("d",$time); //本月查的天数               if($yday < $bday) {               $etime = date("Y-m",strtotime($stime))."-".$yday." 23:59:59";               } else if($byday == $bday) {               $etime = date('Y-m', strtotime($stime))."-".$yday." 23:59:59";               } else {               $etime = date('Y-m-d', strtotime('-1 month',$time))." 23:59:59";               }               break;            case 4:               $season = ceil((date('n',$time))/3);               $stime = date('Y-m-d', mktime(0, 0, 0,$season\*3-3+1,1,date('Y',$time)));               $etime = date("Y-m-d",$time)." 23:59:59";               break;            case 5:               $season = ceil((date('n',$time))/3)-1;//上季度是第几季度               $stime = date('Y-m-d', mktime(0, 0, 0,$season\*3-3+1,1,date('Y',$time)));               $xetime = date('Y-m-d', mktime(23,59,59,$season\*3,date('t',mktime(0, 0 , 0,$season\*3,1,date("Y",$time))),date('Y',$time)))."\\n";               $year = date("z" , mktime(23,59,59,12,31,date("Y",$time))) + 1;               $dtime = date("m-d",$time);               $petime = strtotime($xetime);               if($year == 366 && $dtime=="02-29") {               $etime = date("Y",$petime)."-11-30"." 23:59:59";               } else if($year == 366 && ($dtime=="05-31" || $dtime=="05-30")) {               $etime = date("Y",$petime)."-2-29"." 23:59:59";               } else if($year == 365 && $dtime=="02-28") {               $etime = date("Y",$petime)."-11-30"." 23:59:59";               } else if($year == 365 && ($dtime=="05-31" || $dtime=="05-30" || $dtime=="05-29")) {               $etime = date("Y",$petime)."-2-28"." 23:59:59";               } else {               $etime = date("Y-m-d", strtotime("-3 month",$time))." 23:59:59";               }               break;            case 6:               $stime = date("Y",$time)."-01-01";               $etime = date("Y-m-d",$time)." 23:59:59";               break;            case 7:               $stime = date("Y",strtotime("-1 year",$time))."-01-01";               $jy = date("Y",$time);               $jyear = date("z" , mktime(23,59,59,12,31,$jy)) + 1;// 今年天数               $syin = strtotime("-1 year",$time); //上一年 int               $qn = date("Y",$syin);               $qyear = date("z" , mktime(23,59,59,12,31,$qn)) + 1; // 去年的天数               $tday = date("m-d",$time);               if(($jyear < $qyear) && $tday == '02-28') {               $etime= date("Y", $syin)."-02-29"." 23:59:59";               } else if(($jyear > $qyear) && $tday == '02-29') {               $etime= date("Y", $syin)."-02-28"." 23:59:59";               } else {               $etime = date("Y-m-d", $syin)." 23:59:59";               }               break;            case 8:               $stime = date("Y-m-d",strtotime("-1 day"));               $etime = date("Y-m-d",strtotime("-1 day"))." 23:59:59";               break;           default:               // 同比上周               if( date('w',$time)== 1) { // 判断今天是一周的第几天                  $stime = date('Y-m-d', strtotime('-7 day', $time));               } else {                  $stime = date('Y-m-d', strtotime('-2 monday', $time));               }               $etime = date('Y-m-d', strtotime('-7 day', $time))." 23:59:59";        }// end of switch        return array('stime' => $stime, 'etime' => $etime);     } ```