2

使用这篇文章中的代码https://stackoverflow.com/a/4312630/257629

尝试使用DatePeriod(). 我的 PHP 版本是 5.4.3,我看不到任何错误。DateTimeandDateInterval似乎返回了正确的对象,但是当将它传递给时,DatePeriod我留下了一个空对象。(debug来自 CakePHP 并输出变量的内容。)

// values passed from form, to a function
// $arrival = 2013-09-05
// $departure = 2013-08-16

$start = new DateTime($arrival);
/*
object(DateTime) {
date => '2013-09-05 00:00:00'
timezone_type => (int) 3
timezone => 'UTC'
}
*/

$interval = new DateInterval('P1D');
/*
object(DateInterval) {
    y => (int) 0
    m => (int) 0
    d => (int) 1
    h => (int) 0
    i => (int) 0
    s => (int) 0
    invert => (int) 0
    days => false
}
*/

$end = new DateTime($departure);
/*
object(DateTime) {
date => '2013-08-16 00:00:00'
timezone_type => (int) 3
timezone => 'UTC'
}
*/

$period = new DatePeriod($start, $interval, $end);

debug($period);
/*
object(DatePeriod) {

}
*/

foreach ($period as $date) {
    echo $date->format('Y-m-d')."\n";
}
4

2 回答 2

1

$end日期在$start日期之前,因为您混淆$arrival$departure变量

于 2013-08-05T12:05:33.487 回答
1
$arrival = 2013-09-05
$departure = 2013-08-16

到达不大于出发。如果你设置$arrival = 2013-08-05. 然后输出将是

2013-08-05
2013-08-06
2013-08-07
2013-08-08
2013-08-09
2013-08-10
2013-08-11
2013-08-12
2013-08-13
2013-08-14
2013-08-15
于 2013-08-05T12:03:31.767 回答