简体   繁体   中英

DatePeriod in php >5.3.0 and <5.3.3

Can I reset period object in php <=5.3.3? I would like to use same period object some times in foreach

<?php

$start = new DateTime('2013-10-01');
$end = new DateTime('2013-10-02');
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

foreach ($period as $p) {
    var_dump($p);
}

reset($period);

foreach ($period as $p) {
    var_dump($p);
}

Demo

You can use function iterator_to_array() PHP >= 5.1.0 , that copies elements of an iterator to array. Then you can use this array multiple times.

// ...
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
$periods = iterator_to_array($period);
// ...

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM