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
https://forums.phpfreaks.com/topic/283227-microtime-returning-incorrect-results/
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';

 

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.

 

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.

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.