1

I wanna ask, how to min or plus a year in php I have a date like this 1992-09-10 then I'm using timespan (codeigniter library) to knowing, how long the date is,

here's the code

$time = '1992-09-10';
$timeago = timespan(strtotime($time));

echo $timeago;

then the result is

24 Years, 1 Month, 3 Weeks, 2 Days, 23 Hours, 51 Minutes 

and I want to decrease (-5) years the result should be

19 Years, 1 Month, 3 Weeks, 2 Days, 23 Hours, 51 Minutes

my code is

$time = '1992-09-10';
$timeago = timespan(strtotime('-5 years', strtotime($time)));

echo $timeago;

but the result is

29 Years, 1 Month, 3 Weeks, 3 Days, 17 Hours, 54 Minutes 

anyone know to make it?

Sorry for my bad english

yulianto saparudin
  • 239
  • 2
  • 9
  • 25

1 Answers1

1

The result you get is perfectly normal : 1992 - 5 years equals 1987, which is indeed 29 years ago.

Just use +5 years instead :

$time = '1992-09-10';
$timeago = timespan(strtotime('+5 years', strtotime($time)));

echo $timeago;
// 19 Years, 1 Month, 4 Weeks, 1 Day, 38 Minutes 
roberto06
  • 3,844
  • 1
  • 18
  • 29
  • @yuliantosaparudin You could also reverse the order of your parameters in the `timespan` function : `$timeago = timespan(strtotime($time), strtotime('-5 years'));` – roberto06 Nov 03 '16 at 08:47
  • Haha, no problem. Keep in mind that the results might be slightly different though, as there might not be the same number of leap years between `1992` and `2011` than between `1997` and `2016`. – roberto06 Nov 03 '16 at 08:56