PHP date_sub() Function Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 two parameters as described below: $object: It is a required parameter that specifies the DateTime object returned by date_create() function.$interval: It is a required parameter that specifies the DateInterval object that we want to subtract.Return Value: It returns a DateTime object after subtracting the interval. Below programs illustrate the date_sub() function: Program 1: php <?php // PHP program to illustrate date_sub() function // Create a Date Object $date = date_create('2018-06-25'); // Subtract 5 years from the 25th of June, 2018 date_sub($date, date_interval_create_from_date_string('5 years')); echo date_format($date, 'Y-m-d') . "\n"; // Create a Date Object $date = date_create('2018-06-25'); // Subtract 5 month from the 25th of June, 2018 date_sub($date, date_interval_create_from_date_string('5 month')); echo date_format($date, 'Y-m-d'). "\n"; // Create a Date Object $date = date_create('2018-06-25'); // Subtract 5 days from the 25th of June, 2018 date_sub($date, date_interval_create_from_date_string('5 days')); echo date_format($date, 'Y-m-d'); ?> Output2013-06-25 2018-01-25 2018-06-20 Program 2: When invalid date is passed the date_sub function return a warning message. php <?php // PHP program to illustrate date_sub function // The date_sub() function returns warning // if we passing invalid date $date = date_create('2018-25-25'); date_sub($date, date_interval_create_from_date_string('5 years')); echo date_format($date, 'Y-m-d') . "\n"; ?> Output: Warning: date_sub() expects parameter 1 to be DateTime, bool given in /home/guest/sandbox/Solution.php on line 8Warning: date_format() expects parameter 1 to be DateTimeInterface, bool given in /ho...Reference: http://php.net/manual/en/function.date-sub.php Comment More infoAdvertise with us Next Article PHP date_sub() Function R R_Raj Follow Improve Article Tags : Web Technologies PHP PHP-date-time PHP-function Similar Reads PHP | DateTime sub() Function 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( DateTim 2 min read PHP substr() function The substr() is a built-in function in PHP that is used to extract a part of string. Syntax: substr(string_name, start_position, string_length_to_cut) Parameters: The substr() function allows 3 parameters or arguments out of which two are mandatory and one is optional. string_name: In this parameter 2 min read PHP substr_compare() Function The substr_compare() function is a built-in function in PHP and it helps to compare two strings from a specified start position upto a specified length. Syntax: int substr_compare($str1, $str2, $startpos, $len, $caseInsensitive) Parameters: This function accepts a total of five parameters out of whi 2 min read PHP | gmp_sub() Function The gmp_sub() is an in-built function in PHP which returns the subtraction of the two GMP numbers.(GNU Multiple Precision: For large numbers) Syntax: gmp_sub($num1, $num2) Parameters: This function accepts two GMP numbers $num1 and $num2 as mandatory parameters shown in the above syntax. These param 2 min read PHP substr_count() Function The substr_count() is a built-in function in PHP and is used to count the number of times a substring occurs in a given string. The function also provides us with an option to search for the given substring in a given range of the index. It is case sensitive, i.e., "abc" substring is not present in 3 min read Like