PHP | DateTimeImmutable modify() Function
Last Updated :
10 Oct, 2019
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
$datetimeImmutable = new DateTimeImmutable( '2019-10-02T00:00:00' );
$newDateTimeImmutable = $datetimeImmutable ->modify( '+5 days' );
echo $newDateTimeImmutable ->format( 'Y-m-d' );
?>
|
Program 2: This program modify the given date with the increment of 2 months.
<?php
$datetimeImmutable = new DateTimeImmutable( '2019-10-02T00:00:00' );
$newDateTimeImmutable = $datetimeImmutable ->modify( '+2 months' );
echo $newDateTimeImmutable ->format( 'Y-m-d' );
?>
|
Reference: https://www.php.net/manual/en/datetimeimmutable.modify.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...