1--TEST-- 2DatePeriod can be used as an IteratorAggregate 3--FILE-- 4<?php 5 6$period = new DatePeriod('R2/2012-07-01T00:00:00Z/P7D'); 7foreach ($period as $i => $date) { 8 echo "$i: ", $date->format('Y-m-d'), "\n"; 9} 10 11echo "\n"; 12foreach ($period->getIterator() as $i => $date) { 13 echo "$i: ", $date->format('Y-m-d'), "\n"; 14} 15 16echo "\n"; 17$iter = $period->getIterator(); 18for (; $iter->valid(); $iter->next()) { 19 $i = $iter->key(); 20 $date = $iter->current(); 21 echo "$i: ", $date->format('Y-m-d'), "\n"; 22} 23 24echo "\n"; 25$iter->rewind(); 26for (; $iter->valid(); $iter->next()) { 27 $i = $iter->key(); 28 $date = $iter->current(); 29 echo "$i: ", $date->format('Y-m-d'), "\n"; 30} 31 32echo "\n"; 33foreach (new IteratorIterator($period) as $i => $date) { 34 echo "$i: ", $date->format('Y-m-d'), "\n"; 35} 36 37// Extension that does not overwrite getIterator(). 38class MyDatePeriod1 extends DatePeriod { 39} 40 41echo "\n"; 42$period = new MyDatePeriod1('R2/2012-07-01T00:00:00Z/P7D'); 43foreach ($period as $i => $date) { 44 echo "$i: ", $date->format('Y-m-d'), "\n"; 45} 46 47// Extension that does overwrite getIterator(). 48class MyDatePeriod2 extends DatePeriod { 49 public function getIterator(): Iterator { 50 return new ArrayIterator([1, 2, 3]); 51 } 52} 53 54echo "\n"; 55$period = new MyDatePeriod2('R2/2012-07-01T00:00:00Z/P7D'); 56foreach ($period as $i => $notDate) { 57 echo "$i: $notDate\n"; 58} 59 60?> 61--EXPECT-- 620: 2012-07-01 631: 2012-07-08 642: 2012-07-15 65 660: 2012-07-01 671: 2012-07-08 682: 2012-07-15 69 700: 2012-07-01 711: 2012-07-08 722: 2012-07-15 73 740: 2012-07-01 751: 2012-07-08 762: 2012-07-15 77 780: 2012-07-01 791: 2012-07-08 802: 2012-07-15 81 820: 2012-07-01 831: 2012-07-08 842: 2012-07-15 85 860: 1 871: 2 882: 3 89