In this tutorial, we will show you how to subtract years from a date using PHP.
We will also provide you with tips on how to avoid running into issues with leap years.
Getting last year’s date using PHP’s strtotime and date functions.
To get last year’s date using the strtotime and date functions, you can use the following code:
//Get last year's date in a YYYY-MM-DD format. $lastYear = date("Y-m-d", strtotime("-1 years")); //On 2019-08-22, this resulted in 2018-08-22. var_dump($lastYear);
In the example above, we passed in “-1 years” as a parameter to strtotime.
Essentially, this tells the strtotime function to subtract one year from the current Unix timestamp.
Passing in “-12 months” will also accomplish the exact same thing:
//You can also get last year's date using -12 months. $twelveMonthsAgo = date("Y-m-d", strtotime("-12 months")); //Also resulted in 2018-08-22. var_dump($twelveMonthsAgo);
Both of these examples will return last year’s date in a YYYY-MM-DD format.
How to get last year’s date using DateTime.
If you’re using PHP version 5.3.0 or above and you want to use an object-oriented approach, then you can use the DateTime and DateInterval objects.
In the following example, we subtract one year from today’s date:
//Current Date & Time. $currentDate = new DateTime(); //Get last year's date using the //DateTime::sub function and DateInterval $lastYearDT = $currentDate->sub(new DateInterval('P1Y')); //Get last year's date string in a YYYY-MM-DD format. $lastYear = $lastYearDT->format('Y-m-d'); //var_dump the result. var_dump($lastYear);
In the example above:
- We created a new DateTime object representing today’s date.
- We used the DateTime::sub function to subtract a DateInterval object of P1Y year from the current year. The P1Y $interval_spec parameter means “a period of 1 year.” If we wanted to subtract 2 years instead of 1, we can change “P1Y” to “P2Y”.
- Finally, we got the date in a YYYY-MM-DD format and printed it out onto the page.
Subtracting years from a given date using the strtotime and date functions.
If there is a specific date that you need to subtract a year from, then you can use the following code snippet:
//Minus one year from a given date. $date = '2019-09-09'; $dateMinusOneYear = date("Y-m-d", strtotime("-1 year", strtotime($date))); //Results in 2018-09-09. var_dump($dateMinusOneYear);
In the example above, we passed in a Unix timestamp representation of our date as the 2nd parameter to strtotime.
Using DateTime to subtract years from a given date.
You can also use the DateTime object to subtract years from a given date:
//New DateTime object with a given date. $dt = new DateTime('2016-02-29'); //Subtract one year. $minusOneYearDT = $dt->sub(new DateInterval('P1Y')); //Get it in a YYYY-MM-DD format. $minusOneYear = $minusOneYearDT->format('Y-m-d'); //var_dump the result - 2015-03-01 var_dump($minusOneYear);
The only real difference between this example and the first DateTime example is that we passed the date in question in as the $time parameter in the constructor.
If you run the code snippet above, you will see it prints out “2015-03-01” instead of “2015-02-29”.
This is because 2015 was not a leap year. As a result, it didn’t have 29 days in February.
In other words, February 29th, 2015 never occurred.
This leads us to our next point.
Do not attempt to subtract the year(s) using basic math.
Here are two bad examples of subtracting a year from a date:
$lastYear = date("Y") - 1; $lastYearDate = date($lastYear . "-m-d"); var_dump($lastYearDate);
The solution above will fail if the current date is February 29th in a leap year.
Here is another bad example:
//Another bad example. $dateString = '2016-02-29'; $exploded = explode("-", $dateString); $lastYear = ($exploded - 1) . "-" . $exploded . "-" . $exploded; //Results in 2015-02-29 var_dump($lastYear);
In the code above, we exploded the date, subtracted one year, and then put the date back together again.
Although this approach seems simple and straight-forward, it also fails to adequately deal with leap years. The above example prints out “2015-02-29”, which is an invalid date.
Ten years ago.
In this example, we will subtract 10 years from today’s date:
//Get 10 years ago in a YYYY-MM-DD format. $tenYearsAgo = date("Y-m-d", strtotime("-10 years")); //On 2019-08-22, this resulted in 2009-08-22. var_dump($tenYearsAgo);
We can also achieve the same result using the DateTime object:
//Current Date & Time. $currentDate = new DateTime(); //Get 10 years ago. $tenYearsAgoDT = $currentDate->sub(new DateInterval('P10Y')); //YYYY-MM-DD format. $tenYearsAgo = $tenYearsAgoDT->format('Y-m-d'); //var_dump the result. var_dump($tenYearsAgo);
Here, we passed in P10Y as the $interval_spec parameter for DateInterval.
Other periods that you can use as the $interval_spec parameter include:
- P3Y: To subtract 3 years.
- P5Y: To subtract 5 years.
- P20Y: To subtract 20 years.
- P50Y: To subtract 50 years.
You get the point.
Related: Subtract days from a PHP date.