Page 1 of 1

Cannot get time difference right

Posted: Sun May 29, 2011 11:33 am
by leon_nerd
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:

Code: Select all

$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.

Re: Cannot get time difference right

Posted: Wed Jun 01, 2011 5:00 pm
by Jade
You need to read the comments in the PHP manual for the time() function and you'll have your answer: http://php.net/manual/en/function.time.php