A one-liner option is:
echo date_create['2011-04-24']->modify['-1 days']->format['Y-m-d'];
Running it on Online PHP Editor.
mktime alternative
If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:
// Today's date
echo date['Y-m-d']; // 2016-03-22
// Yesterday's date
echo date['Y-m-d', mktime[0, 0, 0, date["m"], date["d"]-1, date["Y"]]]; // 2016-03-21
// 42 days ago
echo date['Y-m-d', mktime[0, 0, 0, date["m"], date["d"]-42, date["Y"]]]; // 2016-02-09
//Using a previous date object
$date_object = new DateTime['2011-04-24'];
echo date['Y-m-d',
mktime[0, 0, 0,
$date_object->format["m"],
$date_object->format["d"]-1,
$date_object->format["Y"]
]
]; // 2011-04-23
Online PHP Editor
❮ PHP Date/Time Reference
Example
Subtract 40 days from the 15th of March, 2013:
Try it Yourself »
Definition and Usage
The date_sub[] function subtracts some days, months, years, hours, minutes, and seconds from a date.
Syntax
date_sub[object, interval]
Parameter Values
object | Required. Specifies a DateTime object returned by date_create[] |
interval | Required. Specifies a DateInterval object |
Technical Details
Returns a DateTime object on success. FALSE on failure |
5.3+ |
❮ PHP Date/Time Reference
Created: December-30, 2021 | Updated: March-29, 2022 It is an important method for subtracting days, weeks, or months from a given date in PHP. The ways like we may use either PHP’s The strtotime[]
Method to Subtract Days in PHPDateTime[]
to Subtract Days in PHPstrtotime
method or
the in-built DateTime
class to do.date[]
and strtotime[]
both function are use in PHP. Which makes it simple to subtract time [hours, minutes, and seconds] from a given date or current time [date].
The date[]
method returns a prepared string after formatting a specific time.
On the other side turns a text formatted DateTime
into a Unix timestamp. date[]
and strtotime[]
can help subtract time from the current time
[date] in PHP.
So here is the way to subtract time from a current DateTime
. Subtract 1
day with PHP form current date:
Using the strtotime[]
Method to Subtract Days in PHP
Sample Code:
Output:
2021-12-06
2021-12-05
Above is an example of subtracting days from the current date by providing the string yesterday to strtotime
.
Using the DateTime[]
to Subtract Days in PHP
Sample Code:
Output:
2021-12-05
We discussed PHP’s old version 5.3.0, using the DateInterval
class. And it represents a date period.
Now, we will discuss P1D
. We define the DateInterval
class’s objects to P1D
, which means one day [one-day period].
The interval can deduct from the given date and time. We can
use P5D
[five days] instead of P1D
[one day] if you want to remove five days instead of one.
Subtract From the Given Date in PHP
Sample Code:
Output:
2020-12-31
Subtract Week From the Given Date in PHP
Using strtotime[]
Sample Code:
Output:
2021-11-29
As we know that from the start time[]
method, we can subtract time,
day, month, and year from a given date.
The $interval
spec argument DateInterval
class for P1W
. Which stands for a time of one week, P1W=one week Period
. Now, If you want to Change P1W
[one week period] to P2W
to deduct two weeks, it will be a great approach.