Jump to content

Cannot get time difference right


leon_nerd

Recommended Posts

Hi,

 

I am trying to get the difference in time in mins, hours etc thingy. Now, for some reason I cannot get the right minutes or hours difference between two time stamps. Here is my code:

 

$postedTime=strtotime($comment_from_db->date_posted);	
$currentTime=time();
$mins=floor(($currentTime-$postedTime)/60);
if($mins<=1){
echo $mins." minute ago by";
}elseif($mins>1){
if($mins>60){
	$hrs=floor($mins/60);
	if($hrs>24){
	}else{
		if($hrs<2){
			echo $hrs. " hour ago by";
		}else{
			echo $hrs. " hours ago by";
		}
	}
}else{														
	echo $mins." minutes ago by";
}
}

 

Now, for a comment posted like 25 mins ago it shows it was posted 4 hours ago. For a comment posted around 10 hours ago it shows it was posted 16 hours ago.

 

What is the problem here?

 

I was doing a test and checked the time in seconds after an interval of 2 mins. Here are the results:

Comment posted: 1306669490

Current time: 1306683920

 

The difference is around 14430. This is not equal to 2 minutes. So, I am starting to think if the time I am getting is wrong? Should it have 10 digits? or less? I am not sure what is going on. Please, advice.

 

Thanks.

Link to comment
Share on other sites

What does echoing $comment_from_db->date_posted show? What does echo date('Y-m-d H:i:s') show?

 

And, strtotime() takes into account the current time zone setting, whereas time() does not, so you might have an extra hour or two (or four) being used in the math.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.