2012-02-16 206 views
0

是否有任何功能將開始日期和結束日期分成$interval天(或月)的塊?例如:在PHP中按時間間隔分割開始日期和結束日期

$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

*(尖)* HTTP:// derickrethans .nl/talks/time-zendcon10.pdf – Gordon 2012-02-16 14:36:15

+2

@戈登謝謝,對不起,我沒有意識到這是一個重複。投票結束。 – gremo 2012-02-16 15:04:14

回答

2

查看關於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

這裏是遍歷天例如,在一個月與其他間隔相應工作

<?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>"; 
} 
?> 
相關問題