PHP在for循环中向数组添加新值

时间:2015-04-07 19:10:06

标签: php arrays loops while-loop

有人可以请某人告诉我我的代码错误,看起来像这样:

$arrayOfDays = array();
    for($dayIterator=$from; $dayIterator < $to; $dayIterator->modify('+1 day')){
        $arrayOfDays[] = $dayIterator;
    }
    print_r($arrayOfDays);

打印出我的结果:

Array ( [0] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [1] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [2] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [3] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [4] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [5] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [6] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [7] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [8] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [9] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [10] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [11] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [12] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [13] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [14] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [15] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [16] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [17] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [18] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [19] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [20] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [21] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [22] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [23] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [24] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [25] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [26] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [27] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) [28] => DateTime Object ( [date] => 2015-04-30 00:00:00 [timezone_type] => 3 [timezone] => Europe/Paris ) ) 

其中$ from设置为月初,$ to设置为月末。 当我在循环中打印$ dayIterator时,它给了我正确的日期,但是在完成整个阵列之后就填满了最后一天! 我不知道它为什么这样工作:(

2 个答案:

答案 0 :(得分:0)

因为你使用相同的对象而不是实例化新的对象。

只需在每次迭代中调用一个新实例:

$dayIterator = new DateTime(/* --- */);

答案 1 :(得分:0)

您可以尝试不同的方法,例如DatePeriod

您需要提供的是:

  • 开始/结束DateTime对象和DateInterval对象。

快速样本:

<?php
$range = new DatePeriod(
    new DateTime('2015-01-01'),
    new DateInterval('P1D'),
    new DateTime('2015-01-31 23:59:59')
);

foreach($range as $dateTime) {
    print $dateTime->format('d M Y') . '<br>';
}
DateInterval中的

P1D 表示 - P eriod 1

第二个日期时间对象中 23:59:59 的原因是,如果要在日期范围中包含最后一个日期,则必须至少为对象添加1秒。< / p>

希望你明白了。