- Object Oriented Style:
DateTime DateTime::sub (DateInterval interval)
- Procedural style:
DateTime date_sub (DateTime $object, DateInterval $interval)
- $object:this parameter contains a DateTime object created by the date_create() function.
- $interval:this parameter contains a DateInterval object.
// PHP illustration program DateTime::sub()
// function
/ / Create a new DateTime() object
$datetime
=
new
DateTime (
’ 2019-10-03’
) ;
// Initialize interval 2 days
$interval
=
’P2D’
;
// Call the sub() function
$datetime
-> sub (
new
DateInterval (
$interval
));
// Get a new date
// format ’ Ymd ’
echo
$datetime
-> format (
’Ymd’
);
?>
Exit:2019-10 -01
Program 2:This program uses the DateTime::sub() function to subtract a specified interval from a date object.
// PHP illustration program DateTime::sub()
// function
// Create a new DateTime object ( )
$datetime
=
new
DateTime (
’2019-10-03’
);
// Initialize spacing
$interval
=
’P2Y5M2DT0H30M40S’
;
// Call the sub() function
$datetime
-> sub (
new
DateInterval (
$interval
));
// Get a new date
// format ’ Ymd H: i: s ’
echo
$datetime
-> format (
’Ymd H: i: s’
);
?>
Exit:2017-04 -30 23:29:20
Link: https: / /www.php.net/manual/en/datetime.sub.php