19

In PHP given a UTC timestamp I would like to add exactly N number of years. This should take into consideration leap years.

Thank you.

Onema
  • 7,331
  • 12
  • 66
  • 102

4 Answers4

62
$newTimestamp = strtotime('+2 years', $timestamp);

Replace "+2 years" as required.

ref: http://php.net/manual/en/function.strtotime.php

Jeff Parker
  • 7,367
  • 1
  • 22
  • 25
9
$date = new DateTime();
$date->add(new DateInterval('P10Y'));

adds 10 years (10Y) to "today". DateTime's only in PHP 5.3, though.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

One thing you should consider when you do this.

$newTimestamp = strtotime('+2 years', $timestamp);

This adds up 2 years ( 720 or 721 days). In case you just want to keep the same day and month and add 2 extra years in the timestamp

you have to use mktime.

Example

$timestamp = mktime(0, 0, 0, $month, $day, $year+2);`
George D.
  • 1,630
  • 4
  • 23
  • 41
  • Now days you should use a library like Carbon to do all this time/date staff in php. It makes it a joy to work with dates. – George D. Jul 09 '16 at 09:53
1
$date    = "1998-08-14";

$newdate = strtotime ( '+2 years' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );

echo $newdate;

echos

2000-08-14
msturdy
  • 10,479
  • 11
  • 41
  • 52
asd
  • 11
  • 1