thepip3r Posted January 24, 2009 Share Posted January 24, 2009 //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. Link to comment https://forums.phpfreaks.com/topic/142229-solved-negative-values-in-simple-time-calculation/ Share on other sites More sharing options...
cwarn23 Posted January 24, 2009 Share Posted January 24, 2009 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); Link to comment https://forums.phpfreaks.com/topic/142229-solved-negative-values-in-simple-time-calculation/#findComment-745149 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2009 Share Posted January 24, 2009 The "true" parameter causes microtime() to return just a msec value. Just subtract the two numbers. Don't use the substr() function. Link to comment https://forums.phpfreaks.com/topic/142229-solved-negative-values-in-simple-time-calculation/#findComment-745156 Share on other sites More sharing options...
thepip3r Posted January 26, 2009 Author Share Posted January 26, 2009 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 Link to comment https://forums.phpfreaks.com/topic/142229-solved-negative-values-in-simple-time-calculation/#findComment-746807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.