When working with PHP, you may need to parse a human-readable relative time specification. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
date_interval_create_from_date_string: Creates an interval like plus two weeks from an English relative-time description.
What Is date_interval_create_from_date_string in PHP?
Parse a human-readable relative time specification. Creates an interval like plus two weeks from an English relative-time description.
Syntax:
date_interval_create_from_date_string(string $datetime): DateInterval|false
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: Relative interval properties can be context-dependent and may not display as a normalized total duration.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$interval = date_interval_create_from_date_string("2 weeks");
echo $interval->format("%d days");
Output:
14 days
The code focuses on the main operation. Creates an interval like plus two weeks from an English relative-time description.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$interval = date_interval_create_from_date_string("3 days");
echo $interval->format("%d days");
Output:
3 days
What changes? Relative interval properties can be context-dependent and may not display as a normalized total duration.
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
$date = new DateTimeImmutable("2026-10-16", new DateTimeZone("UTC"));
$interval = date_interval_create_from_date_string("10 days");
if ($interval === false) {
throw new InvalidArgumentException("Invalid interval");
}
echo $date->add($interval)->format("Y-m-d");
Output:
2026-10-26
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Creates an interval like plus two weeks from an English relative-time description.
Important Behavior and Edge Cases
- Core behavior: Creates an interval like plus two weeks from an English relative-time description.
- Inputs and results: Relative interval properties can be context-dependent and may not display as a normalized total duration.
- Alternatives: new DateInterval("P10D") uses strict ISO-8601 interval notation.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
date_interval_create_from_date_string | Parse a human-readable relative time specification. |
| Related PHP approach | new DateInterval("P10D") uses strict ISO-8601 interval notation. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Relative interval properties can be context-dependent and may not display as a normalized total duration. |
| Confusing this API with a related operation | new DateInterval("P10D") uses strict ISO-8601 interval 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
- date_interval_create_from_date_string helps you parse a human-readable relative time specification.
- Creates an interval like plus two weeks from an English relative-time description.
- Relative interval properties can be context-dependent and may not display as a normalized total duration.
Use date_interval_create_from_date_string when you need to parse a human-readable relative time specification. 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.