time comparison off by 1 minute ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

time comparison off by 1 minute ???

Post 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
Last edited by kingconnections on Wed Dec 27, 2006 5:09 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
Post Reply