是否有任何功能可以将开始日期和结束日期分成$interval
天(或几个月)的块?例如:
$interval = new DateInterval('P10D');
$start = new DateTime('2012-01-10');
$end = new DateTime('2012-02-16');
$chunks = splitOnInterval($start, $end, $interval);
// Now chunks should contain
//$chunks[0] = '2012-01-10'
//$chunks[1] = '2012-01-20'
//$chunks[2] = '2012-01-30'
//$chunks[3] = '2012-02-09'
//$chunks[3] = '2012-02-16'
我认为DatePeriod
可以提供帮助,但我没有找到任何方法可以使用它。
答案 0 :(得分:3)
在how to iterate over valid calender days上查看此文章。
在php中就像是,
$start = strtotime('2012-01-10');
$end1 = strtotime('2012-02-16');
$interval = 10*24*60*60; // 10 days equivalent seconds.
$chunks = array();
for($time=$start; $time<=$end1; $time+=$interval){
$chunks[] = date('Y-m-d', $time);
}
答案 1 :(得分:1)
这是一个迭代几天的例子,上个月与其他区间
相应地工作<?php
$begin = new DateTime( '2012-11-01' );
$end = new DateTime( '2012-11-11' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}
?>