The DateTime::modify() function is an inbuilt function in PHP which is used to modify or can alter the timestamp of a DateTime object.
Syntax:
php
php
- Object oriented style:
DateTime DateTime::modify( string $modify )
- Procedural style:
DateTime date_modify( DateTime $object, string $modify )
- $object: It specifies the DateTime object returned by date_create() function. This object is modified by DateTime::modify() function.
- $modify: It specifies the date/time string. It is incremented or decremented to modify the DateTime object.
<?php
// PHP program to illustrate
// DateTime::modify() function
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
// Calling of date DateTime::modify() function
// with the increment of 5 days as parameters
$datetime->modify('+5 day');
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
?>
Output:
Program 2:
2019-10-05
<?php
// PHP program to illustrate the
// DateTime::modify() function
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
// Calling of date DateTime::modify() function
// with the increment of 5 months as parameters
$datetime->modify('+5 month');
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
?>
Output:
Reference: https://www.php.net/manual/en/datetime.modify.php2020-03-01