Rusty3 Posted September 3, 2009 Share Posted September 3, 2009 How can I detect half a second has passed? I need to compare two times, a moment in the past and now. (Can not use a waiting routine) Can't figure it out how to do it with microtime, because once I get past one second the diffence would not be accurate. Thanks for any clue. Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/ Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 Can't figure it out how to do it with microtime, because once I get past one second the diffence would not be accurate. That doesn't make sense. Why wouldn't it be accurate? Or rather, why would it be too inaccurate? Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911811 Share on other sites More sharing options...
Rusty3 Posted September 3, 2009 Author Share Posted September 3, 2009 Can't figure it out how to do it with microtime, because once I get past one second the diffence would not be accurate. That doesn't make sense. Why wouldn't it be accurate? Or rather, why would it be too inaccurate? Because microsecs are allways part of a second. So, you can only compare them inside the same second, right? If the time is longer than one second you could be comparing the microseconds of one second with the microseconds of another second! That's my problem! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911814 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 $start = microtime(true); usleep(500000); if (microtime(true) - $start >= 0.5) { echo 'Half a second has passed.'; } Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911817 Share on other sites More sharing options...
Rusty3 Posted September 3, 2009 Author Share Posted September 3, 2009 THANKS Daniel, it works! Nice trick that microtime(true)!!! $start = microtime(true); sleep(3); if (microtime(true) - $start >= 2) { echo 'More than 2 sec had passed.'; } echo (microtime(true) - $start); Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911820 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 Note that the microtime(true) - $start in your if statement and the one you are echoing are not the same. Time will have passed between these two. If you want it to be the same you'll have to assign it to a variable and use that instead. Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911821 Share on other sites More sharing options...
Psycho Posted September 3, 2009 Share Posted September 3, 2009 To expand on what DanielO provided, PHP5 allows for an optional parameter when using microtime() which will return the value as a float. Otherwise, microtime returns a string in the format: "[milliseconds in decimal format] [seconds since epoc]" Which is not as useful, IMHO. If you are using PHP5, then add the optional parameter when using microtime. Otherwise, create a function to do it for you. <?php function mtime_to_float($mtime) { list($microseconds, $seconds) = explode(' ', $mtime); return ($seconds + $microseconds); } $start = microtime(); usleep(500000); $end = microtime(); $time_passed = (mtime_to_float($end) - mtime_to_float($start)); if (($time_passed) > .5) { echo "Process took more than 1/2 second passed."; } else { echo "Process took 1/2 second or less"; } ?> Note: For accuracy do not call microtime() within functionality to do the calculations/comparisons. Instead set variables to microtime() at the points in the code to be evaluated, then do the calculations. Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911822 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 Otherwise, create a function to do it for you upgrade. Fixed. Quote Link to comment https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.