Jump to content

Microtime Returning Incorrect Results


onggie

Recommended Posts

Hi am just learning PHP and I'm building a website for my work that does some monitoring. I seem to be having a strange issue when using the microtime function in PHP. On my index.php I have the following

$.ajax({
url:'loadtime.php',
datatype:"application/json",
type:'get',
data: "host=http://www.mywebsite.com",
success:function(data){
document.getElementById('loadtime_com').innerHTML = data;
},
error:function(){
// code for error
}
});

On loadtime.php

$host = $_GET['host'];

$time = microtime( TRUE );
file_get_contents( $host );
$time = microtime( TRUE ) - $time;
echo $time;

When going to my index.php it shows a time of anything under 2.00 seconds (which is wrong). I then created another PHP file called loadtime2.php and changed the code to

$host = "http://www.mywebsite.com";

$time = microtime( TRUE );
file_get_contents( $host );
$time = microtime( TRUE ) - $time;
echo $time;

And then test the script by going to mywebsite.com/loadtime2.php and this gives me times over 5.00 seconds. I can't work out what is causing this discrepancy, it's like microtime is giving me the time to retrieve loadtime.php from index.php instead of the time to get the website contents.

 

Link to comment
Share on other sites

what php version? because microtime(true) only produces values that can be directly subtracted in php5.0 or above.

Thanks for the reply. I'm on a vanilla CentOS 6 server. So by default it's running 5.3.3.

 

Edit: Spelling

Edited by onggie
Link to comment
Share on other sites

Don't know if this helps but this is what I used years ago

 

function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

$start_time = getmicrotime(); 

//  do whatever like a database query

$end_time = getmicrotime();

echo '<span class="bold">'.substr($end_time-$start_time,0,5).'</span> seconds';
Link to comment
Share on other sites

 

Don't know if this helps but this is what I used years ago

 

function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

$start_time = getmicrotime(); 

//  do whatever like a database query

$end_time = getmicrotime();

echo '<span class="bold">'.substr($end_time-$start_time,0,5).'</span> seconds';

 

 

Thanks, I will give this a try.  I can't tell if it's working at the moment as all the services are running correctly.  But hopefully I can see in the next couple of days.

Link to comment
Share on other sites

 

Don't know if this helps but this is what I used years ago

 

function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

$start_time = getmicrotime(); 

//  do whatever like a database query

$end_time = getmicrotime();

echo '<span class="bold">'.substr($end_time-$start_time,0,5).'</span> seconds';

 

Unfortunately this doesn't seem to work, on my loadtime2.php script which just has the timing is showing between 1.5 - 2.5 seconds and the index.php is showing 0.86 seconds.

Any others have an idea what could be the solution to this problem.  It is mind boggling.

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.