This question is based on another StackOverFlow question, which is entitled Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…
The ticked answer was unexplained but showed this function:
function time_elapsed_string($ptime)
{
$etime = time() - $ptime;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
The time format I have is YYYY-MM-DD HH-MM-SS, for example: 2015-01-21 19:23:09.
My attempt:
echo $target_time = time_elapsed_string(strtotime("2015-01-21 19:23:09"));
It gives:
45 years ago.
Any time is giving also 45 years old. I don't know how the function works, but any help is appreciated.