Page 1 of 1

time comparison off by 1 minute ???

Posted: Wed Dec 27, 2006 4:39 pm
by kingconnections
ok so i am coding something that shows minutes remaining for a server to do something.
The code seems to work fine, except that for the fact that it is always 1 minute off for some reason. Any help would be greatly appreciated. I did a ghetto fix of:

Code: Select all

$diff_time=($stop_time -$start_time)+1;
so here is the code:

Code: Select all

$start_time=time();

$stop_time=strtotime($val);	// value pulled from sql database

	echo "<br>start: ".date('h:i:s',$start_time);
  	echo "<br>stop: ".date('h:i:s',$stop_time);
   	$diff_time=date('i',$stop_time) - date('i',$start_time);
   	echo "<br>diff: ".$diff_time;
   	if ($diff_time < 0) // added this time to account for negitive number.   But still off by 1 minute.
   	{
   		$diff_time='0';
   	}
  	$diff_time=$diff_time;
output of script =


start: 04:37:13
stop: 04:35:00
diff: 57

Servers in Maintenance Mode

Name -----------------StartTime-------------------------- EndTime---------- Time Remaining----------Remove
BATMAN----------Dec 27 2006 4:20PM-------- Dec 27 2006 4:35PM----------58----------------------Button

Posted: Wed Dec 27, 2006 5:07 pm
by feyd
I wouldn't recommend using date() for displaying time differences. Instead I'd calculate it, similarly to printf's (username, not the function) posted snippet (hint hint).

The reason why is because of the behaviors of date() for extremely low values such as those that are likely to be generated when doing a time difference.

Additionally, strtotime() may be interpreting the time wrong due to various reasons. How are the times stored in the database? If they aren't unix timestamps, would it be possible to try using them?