PHP | DateTimeImmutable modify() Function

The DateTimeImmutable::modify() function is an inbuilt function in PHP which is used to modify or alter the timestamp of the created DateTimeImmutable object.

Syntax:

DateTimeImmutable DateTimeImmutable::modify( string $modify )

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



  • object: This parameter holds the DateTime object returned by date_create() function.
  • $modify This parameter holds the date/time string which is set of times to alter the given DataTimeImmutable object.

Return Values: This function returns the modified DateTimeImmutable object on success or False on failure.

Below programs illustrate the DateTimeImmutable::modify() function in PHP:

Program 1: This program modify the given date with the increment of 5 days.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate DateTimeImmutable::modify()
// function
    
// creating a DateTime object
$datetimeImmutable = new DateTimeImmutable('2019-10-02T00:00:00');
  
// Calling of date DateTimeImmutable::modify() function
// with the increment of 5 days as parameters
$newDateTimeImmutable = $datetimeImmutable->modify('+5 days');
  
// Getting the modified date in "y-m-d" format
echo $newDateTimeImmutable->format('Y-m-d');
?>

chevron_right


Output:

2019-10-07

Program 2: This program modify the given date with the increment of 2 months.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate DateTimeImmutable::modify()
// function
    
// Creating a DateTime object
$datetimeImmutable = new DateTimeImmutable('2019-10-02T00:00:00');
  
// Calling of date DateTimeImmutable::modify() function
// with the increment of 2 months as parameters
$newDateTimeImmutable = $datetimeImmutable->modify('+2 months');
  
// Getting the modified date in "y-m-d" format
echo $newDateTimeImmutable->format('Y-m-d');
?>

chevron_right


Output:

2019-12-02

Reference: https://www.php.net/manual/en/datetimeimmutable.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.