使用这篇文章中的代码 https://stackoverflow.com/a/4312630/257629
尝试使用 DatePeriod()
时,我得到一个空对象。我的 PHP 版本是 5.4.3,我看不到任何错误。 DateTime
和 DateInterval
似乎返回了正确的对象,但是当将其传递给 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";
}
最佳答案
$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
关于php - DatePeriod 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18057554/