PHP | DateTime sub() Function Last Updated : 10 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The DateTime::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 DateTime object. Syntax: Object oriented style: DateTime DateTime::sub( DateInterval interval ) Procedural style: DateTime date_sub( DateTime $object, DateInterval $interval ) Parameters: This function uses two parameters as mentioned above and described below: $object: This parameter holds the DateTime object created by date_create() function. $interval: This parameter holds the DateInterval object. Return Values: This function returns the DateTime object after subtraction is done on success or False on failure. Below programs illustrate the DateTime::sub() function in PHP: Program 1: This program uses DateTime::sub() function to subtract 2 days from given date object. php <?php // PHP program to illustrate DateTime::sub() // function // Creating a new DateTime() object $datetime = new DateTime('2019-10-03'); // Initialising a interval of 2 days $interval = 'P2D'; // Calling the sub() function $datetime->sub(new DateInterval($interval)); // Getting a new date time // format of 'Y-m-d' echo $datetime->format('Y-m-d'); ?> Output: 2019-10-01 Program 2: This program uses DateTime::sub() function to subtract the given interval from date object. php <?php // PHP program to illustrate DateTime::sub() // function // Creating a new DateTime() object $datetime = new DateTime('2019-10-03'); // Initialising an interval $interval = 'P2Y5M2DT0H30M40S'; // Calling the sub() function $datetime->sub(new DateInterval($interval)); // Getting a new date time // format of 'Y-m-d H:i:s' echo $datetime->format('Y-m-d H:i:s'); ?> Output: 2017-04-30 23:29:20 Reference: https://www.php.net/manual/en/datetime.sub.php Comment More infoAdvertise with us Next Article PHP | DateTime sub() Function K Kanchan_Ray Follow Improve Article Tags : Web Technologies PHP PHP-date-time PHP-function Similar Reads PHP date_sub() Function The date_sub() function is an inbuilt function in PHP that is used to subtract days, months, years, hours, minutes, and seconds from given date. This function returns a DateTime object on success and FALSE on failure. Syntax: date_sub($object, $interval)Parameters: The date_sub() function accepts tw 2 min read PHP | DateTimeImmutable::sub() Function 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 i 2 min read PHP | DateTime setDate() Function The DateTime::setDate() function is an inbuilt function in PHP which is used to reset the current date of DateTime object with the given date-time object. Syntax: Object oriented style: DateTime DateTime::setDate( int $year, int $month, int $day ) Procedural style: DateTime date_date_set( DateTime $ 2 min read PHP | DateTime add() Function 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( DateT 2 min read PHP | date_date_set() Function The date_date_set() function is an inbuilt function in PHP which is used to set a new Date. This function has four parameters $object, $year, $month and $day and returns DateTime object on success or false on failure. The date_date_set() function is an alias of DateTime::setDate() function. Syntax: 2 min read Like