2

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?

  • `12:09:35 2013-02-08 02:18:52` - what is $time and what is $now here? – Viktor S. Feb 08 '13 at 11:33
  • I'm pretty sure that there is roughly 10 hours difference between `02:18:52` and `12:09:35`. Don't know where you get the 2 hours from. – Mchl Feb 08 '13 at 11:46
  • Oh yeah, i wasn't using 24hrs you know, never knew it was 14:00hrs for 02:18:52, thought it was two pm, pretty dumb of me! –  Feb 08 '13 at 12:38
  • @FAngel $now is the current time and $time is the time gotten from the database, now is 12:09:35 2013-02-08 –  Feb 08 '13 at 12:40
  • @Mchl the thing is, the time for date(), i.e. the current time is twelve hours behind, and since the 24hr system is being used, i confused 02:18:52 for 14:18:52. –  Feb 08 '13 at 12:55
  • i added this: $now=strtotime($now)+(12*60*60) and it worked fine, to deal with it being 12hours behind. –  Feb 08 '13 at 12:58

2 Answers2

2

The problem is that your mysql server and your php.ini or on different time zones. Stick with one. If you cannot change your servers time zone, then all you have to do is get the value back from the datetime field from mysql and then using PHP convert it back into a UNIX timestamp.

// Start by setting the datetime zone for all your pages in PHP
date_default_timezone_set('Europe/Lisbon');

//Extract the datetime from the database regardless of the databases timezone.
$time = '2012-07-02 22:30:52';

//Then in PHP convert it into a UNIX timestamp.
$timestamp = strtotime($time);

Now all your code and times stay relative to the time zone that you specified in your date_default_timezone_set function.

Zevi Sternlicht
  • 5,399
  • 19
  • 31
-2

The best way is simply to not use the function at all, because no one really wants it. Just show the time when the comment was made. (If some one really wants the info: The clock for comparison is in the bottom right corner of the screen).