HomeLanguagesPHP | DateTime modify() Function

PHP | DateTime modify() Function

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

Syntax:

  • Object oriented style:
    DateTime DateTime::modify( string $modify )
  • Procedural style:
    DateTime date_modify( DateTime $object, string $modify )

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

  • $object: It specifies the DateTime object returned by date_create() function. This object is modified by DateTime::modify() function.
  • $modify: It specifies the date/time string. It is incremented or decremented to modify the DateTime object.

Return Value: This function returns the modified DateTime object on success or False on failure.

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

Program 1 :




<?php
// PHP program to illustrate 
// DateTime::modify() function
    
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
  
// Calling of date DateTime::modify() function
// with the increment of 5 days as parameters
$datetime->modify('+5 day');
  
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
  
?>


Output:

2019-10-05

Program 2:




<?php
// PHP program to illustrate the
// DateTime::modify() function
    
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
  
// Calling of date DateTime::modify() function
// with the increment of 5 months as parameters
$datetime->modify('+5 month');
  
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
  
?>


Output:

2020-03-01

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

RELATED ARTICLES

Most Popular

Dominic
32541 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6920 POSTS0 COMMENTS
Nicole Veronica
12036 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7055 POSTS0 COMMENTS
Ted Musemwa
7292 POSTS0 COMMENTS
Thapelo Manthata
7014 POSTS0 COMMENTS
Umr Jansen
6999 POSTS0 COMMENTS