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:
RESETRUNFULL
RESETRUNFULL
<!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>Eg. Using DateInterval::createFromDateString:
RESETRUNFULL
RESETRUNFULL
<!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>