date_subSUBTRACTS an amont of days, months, years, hours, minutes and seconds to a
DateTime object.
<?php
/* - - - - - - - - - - - - - - - - -
Object oriented style
- - - - - - - - - - - - - - - - - */
DateTime public DateTime::sub ( DateInterval $interval )
where,
$interval = A DateInterval object
?>
<?php
/* - - - - - - - - - - - - - - - - -
Procedural style
- - - - - - - - - - - - - - - - - */
DateTime date_sub ( 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.
EXERCISE
<?php
if(class_exists("DateTime") &&
class_exists("DateTimeZone") &&
class_exists("DateInterval"))
{
$dtm_01 = new DateTime("UTC");
$dtm_02 = new DateTime("America/Los_Angeles");
echo '<br>public DateTime add<br><br>';
$dti = new DateInterval('P4Y3M20D');
// P = Period 4Y = 4 years 3M = 3 monts 20D = 20 days
$dtm_01_add = $dtm_01->add($dti);
echo 'From this moment the date will have the following value:<br>';
foreach((array)$dtm_01->add($dti) as $dt01 => $d01)
{
echo $dt01 . ' = ' . $d01 . '<br>';
}
echo '<br>public DateTime sub<br><br>';
$nw_dti = new Dateinterval('P3Y2M10D');
// P = Period 3Y = 3 years 2M = 2 monts 10D = 10 days
$dtm_01_sub = $dtm_01->sub($nw_dti);
echo 'From this moment the date will have the following value:<br>';
foreach((array)$dtm_01->sub($nw_dti) as $nwdt01 => $nwd01)
{
echo $nwdt01 . ' = ' . $nwd01 . '<br>';
}
}
else
{
echo "The required classes are not available in this system.";
}
?>
EXERCISE
<?php
if(class_exists("DateTimeImmutable") &&
class_exists("DateTimeZone") &&
class_exists("DateInterval"))
{
$dtm_01B = new DateTimeImmutable("UTC");
$dtm_02B = new DateTimeImmutable("America/Los_Angeles");
echo '<br>public DateTime add<br><br>';
$dti = new DateInterval('P4Y3M20D');
$dtm_01B_add = $dtm_01B->add($dti);
echo 'From this moment the date will have the following value:<br>';
foreach((array)$dtm_01B->add($dti) as $dt01B => $d01B)
{
echo $dt01B . ' = ' . $d01B . '<br>';
}
echo '<br><br>public DateTime sub<br><br>';
$nw_dti = new Dateinterval('P3Y2M10D');
$dtm_01B_sub = $dtm_01B->sub($nw_dti);
echo 'From this moment the date will have the following value:<br>';
foreach((array)$dtm_01B_sub as $nwdt01B => $nwd01B)
{
echo $nwdt01B . ' = ' . $nwd01B . '<br>';
}
// print_r($dtm_01B_sub);
}
else
{
echo "The required classes are not available in this system.";
}
?>
EXERCISE
<?php
date_default_timezone_set('UTC');
// Initialise all required variables
$startDate = 'now';
$format = 'Y-m-d H:i:s';
$intervals = [
'P3Y6M4DT12H30M5S',
'P0D',
'P2DT1M',
'P1Y2MT23H43M150S'
];
$d = new DateTime($startDate);
echo 'Start date:<br>' . $d->format($format);
echo '<br><br>';
foreach($intervals as $k => $interval) {
date_sub($d, new DateInterval($interval) );
echo $intervals[$k] . ': <br>' . $d->format($format);
echo '<br><br>';
}
?>