Jump to content

[SOLVED] Negative values in simple time calculation??


thepip3r

Recommended Posts

//Top section of code
$timeStart = microtime(true);


//Hundreds of lines of code...

//Bottom section of code
$timeEnd = microtime(true);
echo substr($timeEnd - $timeStart, 0, 6);

 

and probably about 1/10 pages is displayed as a negative value??  Can anyone shed some light on why that might be?? TIA.

I have tested your script and it works perfectly for me. Make sure that your $timeStart variable is not changed during the comment saying Hundreds of lines of code... Also try running the following as a test script and you will see what I meen:

//Top section of code
$timeStart = microtime(true);
sleep(1);
$timeEnd = microtime(true);
echo substr($timeEnd - $timeStart, 0, 6);

it did have to do with substr() but it wasn't entirely the problem.  The negative values were being added when the normal msec calculation was naturally only shorter than six characters (ie.  0.012, 0.0035).  Those values were getting the negatives where the longer numeric strings were being substr()'d correctly.  =D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.