Jump to content

[SOLVED] Half a second!


Rusty3

Recommended Posts

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.

Link to comment
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.