8

PHP | DateTimeImmutable modify() Function

 3 years ago
source link: https://www.geeksforgeeks.org/php-datetimeimmutable-modify-function/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
DateTimeImmutable modify() Function
Related Articles
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.

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');
?>
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');
?>
Output:
2019-12-02

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK