Re: a little math

From: Date: Fri, 20 Apr 2007 09:37:09 +0000
Subject: Re: a little math
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-253330@lists.php.net to get a copy of this message
Edward Kay wrote:
maybe someone can figure why sometimes i get negative values for seconds.. $job['finished'] and $job['finished'] are both unix timestamp. i subtract both to get how many seconds some thing took.. well you should be able to read the rest. sometimes it works fine but other times i get negative seconds. $job['run_time'] = ($job['finished'] - $job['started']); $minutes = number_format($job['run_time'] / 60, 0, '', ''); // seconds left after calculating minutes $seconds = (($job['run_time']- 60) * $minutes); $job['runtime'] = $minutes . ' mins ' . $seconds . ' secs';
    
If $job['run_time'] < 60 then clearly $seconds will be negative. Why don't you use: $minutes = floor($job['run_time'] / 60); $seconds = $job['run_time'] % 60; Edward yeah that seems more simple, but i do check if it's less than 60 seconds.
$job['run_time'] = ($job['finished'] - $job['started']); if ($job['run_time'] < 60) {
       $job['runtime'] = $rsync['run_time'] . ' secs';
} else {
       $minutes = number_format($job['run_time'] / 60, 0, '', '');
       $seconds = (($job['run_time'] - 60) * $minutes);
       $job['runtime'] = $minutes . ' mins ' . $seconds . ' secs';
}

« previous php.general (#253330) next »