使用这篇文章中的代码https://stackoverflow.com/a/4312630/257629
尝试使用DatePeriod()
. 我的 PHP 版本是 5.4.3,我看不到任何错误。DateTime
andDateInterval
似乎返回了正确的对象,但是当将它传递给时,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";
}