MENU
Interval
Synopsis for the class DateInterval:| Properties |
|---|
| int $y |
| int $m |
| int $d |
| int $h |
| int $i |
| int $s |
| int $invert |
| mixed $days |
| Methods |
| __construct(string $interval_spec) |
| static DateInterval createFromDateString(string $time) ALIAS date_interval_create_from_date_string(……) |
| string format(string $format) ALIAS date_interval_format(……) |
For DateInterval, the following characters are recognized in $interval_spec:
| P | All format strings start with P |
| Y | Years |
| M | Months |
| D | Days |
| W | Weeks |
| T | Any time portion is preceded by T |
| H | Hours |
| M | Minutes |
| S | Seconds |
Eg. Using DateInterval:
<!DOCTYPE html><html><head></head>
<body>
<?php
$dt = new DateTime();
$di = new DateInterval('P3Y3M3DT3H3M3S');
echo $dt->format("r")."<br />";
$dt->add($di);
echo $dt->format("r")."<br />";
date_sub($dt,$di); // alias
echo $dt->format("r")."<br />";
$dt2 = new DateTime('2000-12-25');
$dd=$dt->diff($dt2);
var_dump($dd);
echo "<br />".$dd->invert."<br />";
// invert==1 means $dt2 occurs before $dt
echo $dd->format("%r%y years %m months %d days %h hours %i minutes %s seconds")."<br />";
echo $dd->format("%r%Y years %M months %D days %H hours %I minutes %S seconds")."<br />";
echo date_interval_format($dd,"%r%a days.")."<br />"; // alias
?></body></html>Tue, 01 Sep 2026 07:13:50 +0200
Tue, 04 Dec 2029 10:16:53 +0100
Tue, 01 Sep 2026 07:13:50 +0200
object(DateInterval)#4 (16) { ["y"]=> int(25) ["m"]=> int(8) ["d"]=> int(7) ["h"]=> int(7) ["i"]=> int(13) ["s"]=> int(50) ["f"]=> float(0.701455) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(1) ["days"]=> int(9381) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
1
-25 years 8 months 7 days 7 hours 13 minutes 50 seconds
-25 years 08 months 07 days 07 hours 13 minutes 50 seconds
-9381 days.
Tue, 04 Dec 2029 10:16:53 +0100
Tue, 01 Sep 2026 07:13:50 +0200
object(DateInterval)#4 (16) { ["y"]=> int(25) ["m"]=> int(8) ["d"]=> int(7) ["h"]=> int(7) ["i"]=> int(13) ["s"]=> int(50) ["f"]=> float(0.701455) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(1) ["days"]=> int(9381) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
1
-25 years 8 months 7 days 7 hours 13 minutes 50 seconds
-25 years 08 months 07 days 07 hours 13 minutes 50 seconds
-9381 days.
Eg. Using DateInterval::createFromDateString:
<!DOCTYPE html><html><head></head>
<body>
<?php
$di=date_interval_create_from_date_string('3600 seconds');
$di=DateInterval::createFromDateString('1 day');
$di=DateInterval::createFromDateString('2 weeks');
$di=DateInterval::createFromDateString('3 months');
$di=DateInterval::createFromDateString('4 years');
$di=DateInterval::createFromDateString('1 year + 1 month');
$di=DateInterval::createFromDateString('1 day + 12 hours');
$dt = new DateTime();
$dt->add($di);
echo $dt->format('H:i:sA');
?></body></html>19:13:50PM