php 取开始时间和结束时间所跨越的所有季度时间
/* * 由起止日期算出其中所跨的季度 和各季度开始结束时间 * params * $st:开始日期 Y-m-d格式 * $et:结束日期 Y-m-d格式 */ function getQuarter($st,$et){ $stime = strtotime($st); $etime = strtotime($et); if($stime > $etime){ $tpTime = $stime; $stime = $etime; $etime = $tpTime; } //取开始时间是属于第几季度 $season_st = ceil((date('n',$stime))/3); $year_st = date('Y',$stime); //取结束时间是属于第几季度 $season_et = ceil((date('n',$etime))/3); $year_et = date('Y',$etime); //echo $year_st.$season_st,$year_et.$season_et; $return = array(); $times = array(); if($year_st == $year_et){ //同一年内 $return[$year_st.'Q'.$season_st] = 0; $times[$year_st.'Q'.$season_st][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$season_st*3-3+1,1,$year_st)); $times[$year_st.'Q'.$season_st][1] = date('Y-m-d H:i:s', mktime(23,59,59,$season_st*3,date('t',mktime(0, 0 , 0,$season_st*3,1,$year_st)),$year_st)); while($season_st != $season_et){ $season_st++; $return[$year_st.'Q'.$season_st] = 0; $times[$year_st.'Q'.$season_st][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$season_st*3-3+1,1,$year_st)); $times[$year_st.'Q'.$season_st][1] = date('Y-m-d H:i:s', mktime(23,59,59,$season_st*3,date('t',mktime(0, 0 , 0,$season_st*3,1,$year_st)),$year_st)); } }else{ for($i=$year_st,$j=$year_et,$i_st=$season_st,$i_et=$season_et;$i<=$j;$i++){ $return[$year_st.'Q'.$i_st] = 0; $times[$year_st.'Q'.$season_st][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$season_st*3-3+1,1,$year_st)); $times[$year_st.'Q'.$season_st][1] = date('Y-m-d H:i:s', mktime(23,59,59,$season_st*3,date('t',mktime(0, 0 , 0,$season_st*3,1,$year_st)),$year_st)); if($i == $year_st){ while($i_st <4){ $i_st++; $return[$i.'Q'.$i_st] = 0; $times[$i.'Q'.$i_st][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$i_st*3-3+1,1,$i)); $times[$i.'Q'.$i_st][1] = date('Y-m-d H:i:s', mktime(23,59,59,$i_st*3,date('t',mktime(0, 0 , 0,$i_st*3,1,$i)),$i)); } } elseif($i != $j){ for($k=1;$k<=4;$k++){ $return[$i.'Q'.$k] = 0; $times[$i.'Q'.$k][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$k*3-3+1,1,$i)); $times[$i.'Q'.$k][1] = date('Y-m-d H:i:s', mktime(23,59,59,$k*3,date('t',mktime(0, 0 , 0,$k*3,1,$i)),$i)); } } else{//同一年了 for($k=1;$k<=$i_et;$k++){ $return[$i.'Q'.$k] = 0; $times[$i.'Q'.$k][0] = date('Y-m-d H:i:s', mktime(0, 0, 0,$k*3-3+1,1,$i)); $times[$i.'Q'.$k][1] = date('Y-m-d H:i:s', mktime(23,59,59,$k*3,date('t',mktime(0, 0 , 0,$k*3,1,$i)),$i)); } } } } return $times; } /* * 取某个季度的开始和结束时间 * $year 年份,如2014 * $season 季度,1、2、3、4 */ function getQuarterDate($year,$season){ $times = array(); $times['st'] = date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,$year)); $times['et'] = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,$year)),$year)); return $times; }
例:
getQuarter('2011-09-08','2014-07-11');
结果:
Array ( [2011Q3] => Array ( [0] => 2011-07-01 00:00:00 [1] => 2011-09-30 23:59:59 ) [2011Q4] => Array ( [0] => 2011-10-01 00:00:00 [1] => 2011-12-31 23:59:59 ) [2012Q1] => Array ( [0] => 2012-01-01 00:00:00 [1] => 2012-03-31 23:59:59 ) [2012Q2] => Array ( [0] => 2012-04-01 00:00:00 [1] => 2012-06-30 23:59:59 ) [2012Q3] => Array ( [0] => 2012-07-01 00:00:00 [1] => 2012-09-30 23:59:59 ) [2012Q4] => Array ( [0] => 2012-10-01 00:00:00 [1] => 2012-12-31 23:59:59 ) [2013Q1] => Array ( [0] => 2013-01-01 00:00:00 [1] => 2013-03-31 23:59:59 ) [2013Q2] => Array ( [0] => 2013-04-01 00:00:00 [1] => 2013-06-30 23:59:59 ) [2013Q3] => Array ( [0] => 2013-07-01 00:00:00 [1] => 2013-09-30 23:59:59 ) [2013Q4] => Array ( [0] => 2013-10-01 00:00:00 [1] => 2013-12-31 23:59:59 ) [2014Q1] => Array ( [0] => 2014-01-01 00:00:00 [1] => 2014-03-31 23:59:59 ) [2014Q2] => Array ( [0] => 2014-04-01 00:00:00 [1] => 2014-06-30 23:59:59 ) [2014Q3] => Array ( [0] => 2014-07-01 00:00:00 [1] => 2014-09-30 23:59:59 ) )
getQuarter('2014-06-28','2014-07-11');
结果:
Array ( [2014Q2] => Array ( [0] => 2014-04-01 00:00:00 [1] => 2014-06-30 23:59:59 ) [2014Q3] => Array ( [0] => 2014-07-01 00:00:00 [1] => 2014-09-30 23:59:59 ) )