-1

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);
?>
Gururaju Hiddenx
  • 199
  • 1
  • 4
  • 19

1 Answers1

0

1422826850 is 02/01/2015 @ 9:40pm (UTC), current is 02/01/2015 @ 1:48pm (UTC).

The humanTiming function can only deal with the past time not the future.

Maybe you can get some idea from https://stackoverflow.com/a/2690541/692076

Community
  • 1
  • 1
silverfox
  • 5,254
  • 1
  • 21
  • 26