PHP DateTimeImmutable :: sub () Function

Syntax:
DateTimeImmutable::sub (interval)
Parameters:this the function takes a intervalof parameters, which is the number of days, months, years, hours, minutes, or seconds to subtract from the given DateTimeImmutable object.Returned values ​​::this function returns the final date and time after subtraction.The programs below illustrate the DateTimeImmutable::sub() function:Program 1 : this the program reduces the number of days by 2.
// PHP program for illustration DateTimeImmutable::sub()
// function 
// Create a new DateTi object meImmutable() $datetimeImmutable = new DateTimeImmutable ( '2019-10-07' );  
// Initialize interval 2 days $interval = 'P2D' ;  
// Calling the DateTimeImmutable::sub() function $a = $datetimeImmutable -> sub ( new DateInterval ( $interval ));  
// Get a new date
// format ' Ymd ' echo $a -> format ( 'Ymd' ); ?>
Output:
2019-10-05 
Program 2 : This program reduces the month by 5.
// PHP illustration program DateTimeImmutable::sub()
// function 
// Create a new DateTimeImmutable() $datetimeImmutable = new DateTimeImmutable ( '2019-10-07' );  
// Initialize interval 5 months $interval = 'P5M' ;  
// Calling the DateTimeImmutable::sub() function $a = $datetimeImmutable -> sub ( new DateInterval ( $interval ));  
// Get a new date
// format ' Ymd ' echo $a -> format ( 'Ymd' ); ?>
Output:
2019-05-07 
Link
https://devdocs.io/php/datetimeimmutable.sub



Get Solution for free from DataCamp guru