6

PHP | DateTime modify() Function

 3 years ago
source link: https://www.geeksforgeeks.org/php-datetime-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
DateTime modify() Function
Related Articles
PHP | DateTime modify() Function
  • Last Updated : 10 Oct, 2019

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK