Open In App

PHP | DateTime modify() Function

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:



Parameters: This function uses two parameters as mentioned above and described below:

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 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 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


Article Tags :