PHP | DateTimeImmutable::sub() Function
Last Updated :
11 Oct, 2019
The DateTimeImmutable::sub() function is an inbuilt function in PHP which is used to subtract a number of days, months, years, hours, minutes and seconds from a created DateTimeImmutable object.
Syntax:
DateTimeImmutable::sub( interval )
Parameters: This function accepts a parameter interval which is the number of days or months or years or hours or minutes or seconds which are going to be subtracted from the given DateTimeImmutable object.
Return Values: : This function returns the final datetime after subtraction is done.
Below programs illustrate the DateTimeImmutable::sub() function:
Program 1: This program decreases the days by 2.
<?php
$datetimeImmutable = new DateTimeImmutable( '2019-10-07' );
$interval = 'P2D' ;
$a = $datetimeImmutable ->sub( new DateInterval( $interval ));
echo $a ->format( 'Y-m-d' );
?>
|
Output:
2019-10-05
Program 2: This program decreases the month by 5.
<?php
$datetimeImmutable = new DateTimeImmutable( '2019-10-07' );
$interval = 'P5M' ;
$a = $datetimeImmutable ->sub( new DateInterval( $interval ));
echo $a ->format( 'Y-m-d' );
?>
|
Output:
2019-05-07
Reference
https://devdocs.io/php/datetimeimmutable.sub
Share your thoughts in the comments
Please Login to comment...