[thelist] Calculating Times in PHP [the resolution]

Poojie poojie at dccnet.com
Tue Jun 12 01:32:19 CDT 2001


Hello me (and everyone wanting the resolution to this problem),

I traced the problem back to the original microtime function:

code:
-------------------------------------------
function time_now(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
-------------------------------------------

$sec is the amount of seconds (a whole number)
$usec is the amount of time below 1 second (a decimal number)

For some reason, occationally $usec would "turn-over" (ie: 0.998,
0.999, 0.000) but $sec would not increase by one at the same time.
So, when I ran this code:

code:
-------------------------------------------
$t_start = time_now();     // Start Time
func_to_time();                // Some action
$t_end = time_now();      // End Time

$t_total = $t_end  - $t_start; // Total time passed
-------------------------------------------

$t_start would actually be greater than $t_end .

Anyhoo, I fixed this by making my own time_now() function:

code:
-------------------------------------------
function time_now(){
    $time = gettimeofday();
    return ((float)$time['usec']/1000000);
}
-------------------------------------------

...which returns values similar to 321.01234 (321 seconds +
0.01234). My version of time_now() is good for timing up 999.999999
seconds, which is way more than enough for benchmarking.


Daryl







More information about the thelist mailing list