Jump to content

[SOLVED] Half a second!


Rusty3

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911814
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/173004-solved-half-a-second/#findComment-911822
Share on other sites

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.