HomeLanguagesPHP | DateTimeImmutable modify() Function

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.




<?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');
?>


Output:

2019-10-07

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




<?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');
?>


Output:

2019-12-02

Reference: https://www.php.net/manual/en/datetimeimmutable.modify.php

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS