Jump to content

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
https://forums.phpfreaks.com/topic/237782-cannot-get-time-difference-right/
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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.