In this tutorial, we will show you how to subtract days, weeks, or months from a date using PHP.
To do this, we can use the strtotime function or the DateTime object.
How to subtract one day from a date using PHP.
To get yesterday’s date, you can pass in the string “yesterday” to the strtotime function like so:
//Yesterday $yesterday = date("Y-m-d", strtotime("yesterday")); //On 2019-08-19, this resulted in 2019-08-18 echo $yesterday;
Passing in the string “yesterday” to strtotime is basically the exact same thing as subtracting “-1 day” from today’s date.
Here is a DateTime alternative, which you can use if your PHP version is 5.3.0 or above:
//New DateTime object representing today's date. $currentDate = new DateTime(); //Use the sub function to subtract a DateInterval $yesterdayDT = $currentDate->sub(new DateInterval('P1D')); //Get yesterday's date in a YYYY-MM-DD format. $yesterday = $yesterdayDT->format('Y-m-d'); //Print it out. echo $yesterday;
PHP introduced the DateInterval object in version 5.3.0. This object represents a date interval.
In the example above, we set the DateInterval object to P1D, which means a period of one day. We then subtracted that interval from the current date.
If you would like to subtract five days instead of one day, then you can use P5D instead of P1D.
If you want to subtract a day from a given date:
//Pass the date you want to subtract from in as //the $time parameter for DateTime. $currentDate = new DateTime('2019-01-01'); //Subtract a day using DateInterval $yesterdayDT = $currentDate->sub(new DateInterval('P1D')); //Get the date in a YYYY-MM-DD format. $yesterday = $yesterdayDT->format('Y-m-d'); //Print it out. echo $yesterday;
In the example above, we subtracted one day from 2019-01-01. This resulted in “2018-12-31”.
Subtract 7 days.
To get last week’s date in PHP, we can use the strtotime function like so:
//7 days ago - last week. $lastWeek = date("Y-m-d", strtotime("-7 days")); //On 2019-08-19, this resulted in 2019-08-12 echo $lastWeek;
In this case, we passed “-7 days” in as a parameter to the strtotime function.
This works because the strtotime function is pretty good at parsing textual date descriptions.
Here is an alternative solution for those of you who prefer to use the DateTime object:
//Today's date. $currentDate = new DateTime(); //Subtract a day using DateInterval $lastWeekDT = $currentDate->sub(new DateInterval('P1W')); //Get the date in a YYYY-MM-DD format. $lastWeek = $lastWeekDT->format('Y-m-d'); //Print out the result. echo $lastWeek;
Above, we set “P1W” as the $interval_spec parameter for DateInterval. This translates into “a period of one week.”
If you want to subtract two weeks instead of one, then you can change “P1W” to “P2W”.
Last month / -30 days.
Getting last month’s date can be tricky. This is because the number of days in a month can vary.
For example, the month of October has 31 days, while September has 30.
Take the following example:
//Last month $lastMonth = date("Y-m-d", strtotime("-1 month")); //On 2019-08-19, this resulted in 2019-07-19 echo $lastMonth;
The snippet above will work in most situations.
However, on October 31st, the function will actually return “October 1st” instead of “September 30th”.
This is because PHP’s strtotime function treats “-1 month” the same as “-30 days.”
If you want to get the previous month’s name or number, then you will need to use the following code instead:
//Get the first date of the previous month. echo date("Y-m-d", strtotime("first day of previous month"));
If you run the script above on October 31st, it will return September 1st.
As you can see, dealing with dates can be very tricky.
Related:
- Add days to a date in PHP.
- Subtract years from a PHP date.
- Subtract hours from a timestamp using PHP.