PHP | DateTime add() Function
Last Updated :
10 Oct, 2019
The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object.
Syntax:
- Object oriented style:
DateTime DateTime::add( DateInterval $interval )
- Procedural style:
DateTime date_add( DateTime $object, DateInterval $interval )
Parameters: This function uses two parameters as mentioned above and described below:
- $object: It specifies the DateTime object returned by date_create() function. This function returns a new DateTime object.
- $interval: This parameter holds the DateInterval object.
Return Value: This function returns the new DateTime object after changing on success or False on failure.
Below programs illustrate the DateTime::add() function in PHP:
Program 1:
<?php
$datetime = new DateTime( '2019-09-30' );
$datetime ->add( new DateInterval( 'P1D' ));
echo $datetime ->format( 'Y-m-d' ) . "\n" ;
?>
|
Program 2:
<?php
$datetime = new DateTime( '2019-09-30' );
$datetime ->add( new DateInterval( 'PT5H3M10S' ));
echo $datetime ->format( 'Y-m-d H:i:s' ) . "\n" ;
?>
|
Output:
2019-09-30 05:03:10
Reference: https://www.php.net/manual/en/datetime.add.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...