date_addAdds an amont of days, months, years, hours, minutes and seconds to a
DateTime object.
<?php
/* - - - - - - - - - - - - - - - - -
Object oriented style
- - - - - - - - - - - - - - - - - */
DateTime public DateTime::add ( DateInterval $interval )
where,
$interval = A DateInterval object
?>
<?php
/* - - - - - - - - - - - - - - - - -
Procedural style
- - - - - - - - - - - - - - - - - */
DateTime date_add ( DateTime $object ,
DateInterval $interval )
where,
$object = A DateTime object returned by date_create
$interval = A DateInterval object
?>
$object
A DateTime object returned by date_create.
$interval
A DateInterval object.
This function returns the DateTime object for method chaining or FALSE on failure.
If used in the procedural style this function modifies the DateTime object.
PREDEFINED date_format CONSTANTS
This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons.
Use DATE_ATOM or DateTimeInterface::ATOM for compatibility with ISO-8601 instead.
EXERCISE
<?php
$edate011 = new DateTime("now");
$intrv011 = new DateInterval('P2D');
// P = Period, 2D = 2 days
echo 'now:<br>' . $edate011->format(DATE_W3C) . '<br><br><br>';
$edate011->add($intrv011);
echo 'two days ahead:<br>' . $edate011->format(DATE_W3C);
?>
EXERCISE
<?php
date_default_timezone_set("UTC");
$edate021 = new DateTime("now");
$intrv021 = new DateInterval('PT2H2M2S');
// P = Period, T = Time, 2H = 2 hours, 2M = 2 minutes 2S = 2 seconds
echo 'now:<br>' . $edate021->format(DATE_W3C) . '<br><br><br>';
$edate021->add($intrv021);
echo '02:02:02 ahead:<br>' .$edate021->format(DATE_W3C);
?>
EXERCISE
<?php
date_default_timezone_set("UTC");
$edate031 = new DateTime("now");
$intrv031 = new DateInterval('PT2H2M2S');
// P = Period, T = Time, 2H = 2 hours, 2M = 2 minutes 2S = 2 sconds
echo 'now:<br>' . $edate031->format(DateTimeInterface::COOKIE) . '<br><br><br>';
$edate031->add($intrv031);
echo '02:02:02 ahead:<br>' .$edate031->format(DATE_COOKIE);
?>
EXERCISE
<?php
if(class_exists("DateTime") &&
class_exists("DateTimeZone") &&
class_exists("DateInterval"))
{
$dtm_041 = new DateTime("UTC");
$dtm_042 = new DateTime("America/Los_Angeles");
// echo '<br>public DateTime add';
$dti = new DateInterval('P4Y3M20D');
// P = Period 4Y = 4 years 3M = 3 months 20D = 20 days
$dtm_041_add = $dtm_041->add($dti);
foreach((array)$dtm_041_add as $dt041 => $d041)
{
echo $dt041 . ' = ' . $d041 . '<br>';
}
// print_r($dtm_041_add);
}
else
{
echo "At least one of the classes does not exist in this system!";
}
?>
EXERCISE
<?php
date_default_timezone_set("Europe/Lisbon");
$edate055 = new DateTimeImmutable("now");
$intrv055 = new DateInterval('P6W');
// P = Period, 6W = 6 weeks
echo $edate055->format(DATE_RSS) . '<br><br>';
foreach((array)$edate055->add($intrv055) as $ed055 => $e055)
{
echo $ed055 . ' = ' . $e055 . '<br>';
}
?>
EXERCISE
<?php
date_default_timezone_set("Asia/Tokyo");
$date = new DateTime('now');
$date->add(new DateInterval('P0Y0DT0H'));
echo $date->format('Y-M-d H:i:s') . "<br>";
?>
EXERCISE
<?php
$date = date_create('now');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, DATE_RFC3339_EXTENDED);
?>
EXERCISE
<?php
date_default_timezone_set('UTC');
// Initialise all required variables
$startDate = 'now';
$format = 'Y-m-d H:i:s';
$intervals = array(
'P3Y6M4DT12H30M5S',
'P0D',
'P2DT1M',
'P1Y2MT23H43M150S'
);
$d = new DateTime($startDate);
echo 'Start date:<br>' . $d->format($format);
echo '<br><br>';
foreach($intervals as $k => $interval) {
date_add($d, new DateInterval($interval) );
echo $intervals[$k] . ': <br>' . $d->format($format);
echo '<br><br>';
}
?>