Subtracting years from a date using PHP.

This is a tutorial on how to subtract years from a date using PHP. In this guide, I will also provide examples of what not to do so that you can avoid any issues with leap years.

Getting last year’s date using the strtotime and date functions.

To get last year’s date using the strtotime and date functions, you can use the following example:

//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.

In the PHP code 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. Note that 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.

Both of these examples return last year’s date in a YYYY-MM-DD format.

Getting last year’s date using DateTime.

If you’re using PHP version 5.3.0 or above and you’re looking for an Object Orientated approach, you can use the DateTime and DateInterval objects.

In the following example, we extract 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.

In the example above:

  1. We created a new DateTime object representing today’s date.
  2. 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 would change “P1Y” to “P2Y”.
  3. 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 you have a specific date and you want to subtract a year from it, 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.

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

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 that “2015-03-01” is printed out instead of “2015-02-29”. This is because 2015 was not a leap year and therefore it didn’t have 29 days in February. i.e. The 29th of February, 2015 never occurred.

This leads me to my 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");

The example above will fail if the current date is the 29th of February in a leap year.

Another bad example:

//Another bad example.
$dateString = '2016-02-29';
$exploded = explode("-", $dateString);
$lastYear = ($exploded[0] - 1) . "-" . $exploded[1] . "-" . $exploded[2];
//Results in 2015-02-29

Here, we exploded the date, subtracted one year and 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 never happened.

Ten years ago.

In this example, I will extract 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.

We can also achieve the same result with 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.

Here, we passed in P10Y as the $interval_spec parameter for DateInterval.

Other periods that you can use as the $interval_spec parameter:

  • 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.