When working with PHP, you may need to obtain a traversable iterator for the period occurrences. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
DatePeriod::getIterator: DatePeriod implements IteratorAggregate and can also be used directly in foreach.
What Is DatePeriod::getIterator in PHP?
Obtain a traversable iterator for the period occurrences. DatePeriod implements IteratorAggregate and can also be used directly in foreach.
Syntax:
$period->getIterator(): Traversable
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Do not assume the returned iterator is a PHP array. Iteration depends on start, interval, end/count and flags.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$period = new DatePeriod(new DateTimeImmutable("2026-01-01"), new DateInterval("P1D"), 3);
foreach ($period->getIterator() as $date) {
echo $date->format("Y-m-d") . PHP_EOL;
}
Output:
2026-01-01
2026-01-02
2026-01-03
2026-01-04
The code focuses on the main operation. DatePeriod implements IteratorAggregate and can also be used directly in foreach.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$period = new DatePeriod(new DateTimeImmutable("2026-01-01"), new DateInterval("P1W"), 2);
$iterator = $period->getIterator();
var_dump($iterator instanceof Traversable);
Output:
bool(true)
What changes? Do not assume the returned iterator is a PHP array. Iteration depends on start, interval, end/count and flags.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$period = new DatePeriod(new DateTimeImmutable("2026-10-01"), new DateInterval("P1D"), 5);
$dates = [];
foreach ($period->getIterator() as $date) {
$dates[] = $date->format("Y-m-d");
}
print_r($dates);
Output:
Array
(
[0] => 2026-10-01
[1] => 2026-10-02
[2] => 2026-10-03
[3] => 2026-10-04
[4] => 2026-10-05
[5] => 2026-10-06
)
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. DatePeriod implements IteratorAggregate and can also be used directly in foreach.
Important Behavior and Edge Cases
- Core behavior: DatePeriod implements IteratorAggregate and can also be used directly in foreach.
- Inputs and results: Do not assume the returned iterator is a PHP array. Iteration depends on start, interval, end/count and flags.
- Alternatives: foreach ($period as $date) iterates directly without calling getIterator() explicitly.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
DatePeriod::getIterator | Obtain a traversable iterator for the period occurrences. |
| Related PHP approach | foreach ($period as $date) iterates directly without calling getIterator() explicitly. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Do not assume the returned iterator is a PHP array. Iteration depends on start, interval, end/count and flags. |
| Confusing this API with a related operation | foreach ($period as $date) iterates directly without calling getIterator() explicitly. |
| Assuming output is identical across environments | Check return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result. |
Key Takeaways
- DatePeriod::getIterator helps you obtain a traversable iterator for the period occurrences.
- DatePeriod implements IteratorAggregate and can also be used directly in foreach.
- Do not assume the returned iterator is a PHP array. Iteration depends on start, interval, end/count and flags.
Use DatePeriod::getIterator when you need to obtain a traversable iterator for the period occurrences. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.