yulianto saparudin yulianto saparudin - 1 month ago 13
PHP Question

PHP How to min or plus year with timespan

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

Answer

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