PHP | DateTime modify() Function

Last Updated : 10 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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:
  • Object oriented style:
    DateTime DateTime::modify( string $modify )
  • Procedural style:
    DateTime date_modify( DateTime $object, string $modify )
Parameters: This function uses two parameters as mentioned above and described below:
  • $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.
Return Value: This function returns the modified DateTime object on success or False on failure. Below programs illustrate the DateTime::modify() function in PHP: Program 1 : php
<?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:
2019-10-05
Program 2: php
<?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:
2020-03-01
Reference: https://www.php.net/manual/en/datetime.modify.php

Next Article

Similar Reads