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:

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:

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:

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 date. 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:

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:

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:

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

Another bad example:

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:

We can also achieve the same result with the DateTime object:

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.

Facebook Comments