When working with PHP, you may need to create an interval from a relative-language description. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
DateInterval::createFromDateString: Returns a DateInterval representing a relative period that can be applied to date objects.
What Is DateInterval::createFromDateString in PHP?
Create an interval from a relative-language description. Returns a DateInterval representing a relative period that can be applied to date objects.
Syntax:
DateInterval::createFromDateString(string $datetime): DateInterval
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: The parser accepts English relative-time expressions, not arbitrary localized strings. User input should be restricted.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$interval = DateInterval::createFromDateString("3 days");
echo $interval->format("%d days");
Output:
3 days
The code focuses on the main operation. Returns a DateInterval representing a relative period that can be applied to date objects.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$date = new DateTimeImmutable("2026-10-16", new DateTimeZone("UTC"));
$interval = DateInterval::createFromDateString("2 weeks");
echo $date->add($interval)->format("Y-m-d");
Output:
2026-10-30
What changes? The parser accepts English relative-time expressions, not arbitrary localized strings. User input should be restricted.
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
$deliveryDate = new DateTimeImmutable("2026-10-16", new DateTimeZone("UTC"));
$shippingEstimates = ["2 days", "5 days", "1 week"];
foreach ($shippingEstimates as $estimate) {
$interval = DateInterval::createFromDateString($estimate);
echo $estimate . ": " . $deliveryDate->add($interval)->format("Y-m-d") . PHP_EOL;
}
Output:
2 days: 2026-10-18
5 days: 2026-10-21
1 week: 2026-10-23
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Returns a DateInterval representing a relative period that can be applied to date objects.
Important Behavior and Edge Cases
- Core behavior: Returns a DateInterval representing a relative period that can be applied to date objects.
- Inputs and results: The parser accepts English relative-time expressions, not arbitrary localized strings. User input should be restricted.
- Alternatives: new DateInterval("P3D") creates an interval using explicit ISO-8601 notation.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
DateInterval::createFromDateString | Create an interval from a relative-language description. |
| Related PHP approach | new DateInterval("P3D") creates an interval using explicit ISO-8601 notation. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | The parser accepts English relative-time expressions, not arbitrary localized strings. User input should be restricted. |
| Confusing this API with a related operation | new DateInterval("P3D") creates an interval using explicit ISO-8601 notation. |
| 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
- DateInterval::createFromDateString helps you create an interval from a relative-language description.
- Returns a DateInterval representing a relative period that can be applied to date objects.
- The parser accepts English relative-time expressions, not arbitrary localized strings. User input should be restricted.
Use DateInterval::createFromDateString when you need to create an interval from a relative-language description. 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.