PHP | date_modify() Function

The date_modify() function is an inbuilt function in PHP. Through the help of this function, we can modify or can alter the timestamp of DateTime object. The DateTime object and string modify are parameters in the calling function.

Syntax:

date_modify(DateTime $object, string $modify);

Parameters:
The function accepts two parameters as described below:



  1. $object : This is a mandatory parameter. It specifies a DateTime object returned by date_create() .This object is modified by the above mentioned function.
  2. $modify : This is also a mandatory parameter. This specifies a date/time string. It is incremented or decremented to modify the DateTime object.

Return Values :
This function returns a DateTime object on success. And returns FALSE on failure.

Below programs illustrate the date_modify() function :

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate date_modify()
// function
    
// creating DateTime object
$date=date_create("2018-04-08");
  
// calling of date modify function
// with two required parameters 
date_modify($date, "+15 days"); 
  
// printing the modified date in "y-m-d" format
echo date_format($date, "Y-m-d");
?>

chevron_right


Output:

2018-04-13

Program 2: This program will be increase months by one

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate date_modify()
// function
  
// creating a DateTime object
$date = date_create('2000-10-14');
  
// calling date_modify function with 
// two required parameters
date_modify($date, '+1 month');
  
// printing the modified date
echo date_format($date, 'Y-m-d');
?>

chevron_right


Output:

2000-11-14

Reference
http://php.net/manual/en/datetime.modify.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.