i have a question on how to properly display the elapsed time after an action was performed, i have a simple commenting system and would like to add the functionality of properly displaying when the comment was made, i did some research and came across this:
function ago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = date('Y-m-d h:i:s');
$now=strtotime($now);
if($now>$time)
$difference = $now - $time;
elseif($time>$now)
$difference = $time - $now;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] ago ";
}
i changed the varibale $now to get the value of the date instead of the time, i have a database that has a table which has a column that deals with time, it's set to datetime so i then convert it using the strtotime function and send it through the function:
$time=$row['time'];
$time=strtotime($time);
$ago=ago($time);
the problem is it returns wrong periods, for example, posting a comment one minute ago returns two hours ago, the time zones are correct and i even print the actual dates and times, for example, for today, i printed this:
10 hours ago 2013-02-08 12:09:35 2013-02-08 02:18:52
as you can see the, difference between the time should be about two hours, not ten. Does anyone have a better idea on how to do this, or can anyone kindly point out where the error is?