是否有任何功能将起始日期和结束日期分为 $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 可以提供帮助,但我没有找到如何使用它的任何方法。

有帮助吗?

解决方案

检查本文 如何迭代有效的安排日.

在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);
}

其他提示

这是迭代几天的一个示例,一个月正在与其他间隔相应地工作

<?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>";
}
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top