I want to get the date_time
data from my database and echo it as in the format -
Example: added 7 hours ago
, added 8 minutes ago
I've used the following code for this approach, but my code prints only the string added ago
, with no elapsed time data in it. The function seems okay, but I don't know where did I mess up my code.
Thanks for any help.
//Calculating Time Elapsed
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$time = strtotime($row['dt']);
$temp = humanTiming($time);
echo 'added ' .$temp. ' ago';
}
mysql_close($conn);
?>