php - DatePeriod 返回空对象

标签 php date

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

尝试使用 DatePeriod() 时,我得到一个空对象。我的 PHP 版本是 5.4.3,我看不到任何错误。 DateTimeDateInterval 似乎返回了正确的对象,但是当将其传递给 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/

相关文章:

javascript - 如何使用 12 小时格式创建 JavaScript 日期

php - 如何在jwplayer中停止视频onclick功能

php - 如何将文本写入 XML 文件?

日期和向量的 R 序列

android - 如何仅在android中获取用户创建日期

java - 是否可以同时使用 TextStyle.SHORT 和 TextStyle.FULL 解析当月的日期?

java - 日期在java中将dd-MMM-yyyy转换为dd-MM-yyyy

php - Laravel 5 Eloquent : How do I sort db rows and also get rank?

php - 如何捕获网络上的击键?

php - 根据各自的日期时间字段从两个不同的表中获取记录 - mysql