DateInterval::createFromDateString
(PHP 5 >= 5.3.0)
DateInterval::createFromDateString — Sets up a DateInterval from the relative parts of the string
Description
Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string.
Parameters
-
time
-
A date with relative parts. Specifically, the relative formats supported by the parser used for strtotime() and DateTime will be used to construct the DateInterval.
Examples
Example #1 Parsing valid date intervals
<?php
// Each set of intervals is equal.
$i = new DateInterval('P1D');
$i = DateInterval::createFromDateString('1 day');
$i = new DateInterval('P2W');
$i = DateInterval::createFromDateString('2 weeks');
$i = new DateInterval('P3M');
$i = DateInterval::createFromDateString('3 months');
$i = new DateInterval('P4Y');
$i = DateInterval::createFromDateString('4 years');
$i = new DateInterval('P1Y1D');
$i = DateInterval::createFromDateString('1 year + 1 day');
$i = new DateInterval('P1DT12H');
$i = DateInterval::createFromDateString('1 day + 12 hours');
$i = new DateInterval('PT3600S');
$i = DateInterval::createFromDateString('3600 seconds');
?>
Return Values
Returns a new DateInterval instance.
Коментарии
Please note that Daylight Saving Time (DST) transitions only work as expected when using new \DateInterval(), not \DateInterval::createFromDateString().
<?php
$now = new \DateTimeImmutable("2025-03-29 13:00:00", new DateTimeZone('Europe/Berlin'));
var_dump($now);
// 2025-03-29 13:00:00
var_dump($now->add(\DateInterval::createFromDateString('1440 minutes')));
// 2025-03-30 13:00:00
var_dump($now->add(new \DateInterval('PT1440M')));
// 2025-03-30 14:00:00
// these are not equivalent:
var_dump(\DateInterval::createFromDateString('1440 minutes'));
var_dump(new \DateInterval('PT1440M'));
?>