onggie Posted October 24, 2013 Share Posted October 24, 2013 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. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 24, 2013 Share Posted October 24, 2013 what php version? because microtime(true) only produces values that can be directly subtracted in php5.0 or above. Quote Link to comment Share on other sites More sharing options...
onggie Posted October 24, 2013 Author Share Posted October 24, 2013 (edited) 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 October 24, 2013 by onggie Quote Link to comment Share on other sites More sharing options...
AdRock Posted October 24, 2013 Share Posted October 24, 2013 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'; Quote Link to comment Share on other sites More sharing options...
onggie Posted October 25, 2013 Author Share Posted October 25, 2013 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. Quote Link to comment Share on other sites More sharing options...
onggie Posted October 28, 2013 Author Share Posted October 28, 2013 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. Quote Link to comment 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.