When working with PHP, you may need to retrieve an explicitly configured end date of a period. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
DatePeriod::getEndDate: Returns null when the period was created using a recurrence count rather than an end date.
What Is DatePeriod::getEndDate in PHP?
Retrieve an explicitly configured end date of a period. Returns null when the period was created using a recurrence count rather than an end date.
Syntax:
$period->getEndDate(): ?DateTimeInterface
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: An end date is normally exclusive unless INCLUDE_END_DATE is set; the value can be null.
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"), new DateTimeImmutable("2026-01-05"));
echo $period->getEndDate()->format("Y-m-d");
Output:
2026-01-05
The code focuses on the main operation. Returns null when the period was created using a recurrence count rather than an end date.
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("P1D"), 3);
var_dump($period->getEndDate());
Output:
NULL
What changes? An end date is normally exclusive unless INCLUDE_END_DATE is set; the value can be null.
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
$start = new DateTimeImmutable("2026-01-01");
$end = new DateTimeImmutable("2026-01-10");
$period = new DatePeriod($start, new DateInterval("P2D"), $end);
$endDate = $period->getEndDate();
echo $endDate === null ? "Count-based period" : $endDate->format("Y-m-d");
Output:
2026-01-10
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Returns null when the period was created using a recurrence count rather than an end date.
Important Behavior and Edge Cases
- Core behavior: Returns null when the period was created using a recurrence count rather than an end date.
- Inputs and results: An end date is normally exclusive unless INCLUDE_END_DATE is set; the value can be null.
- Alternatives: DatePeriod::getStartDate() retrieves the starting date.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
DatePeriod::getEndDate | Retrieve an explicitly configured end date of a period. |
| Related PHP approach | DatePeriod::getStartDate() retrieves the starting date. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | An end date is normally exclusive unless INCLUDE_END_DATE is set; the value can be null. |
| Confusing this API with a related operation | DatePeriod::getStartDate() retrieves the starting date. |
| 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::getEndDate helps you retrieve an explicitly configured end date of a period.
- Returns null when the period was created using a recurrence count rather than an end date.
- An end date is normally exclusive unless INCLUDE_END_DATE is set; the value can be null.
Use DatePeriod::getEndDate when you need to retrieve an explicitly configured end date of a period. 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.