Displaying page loading time dynamically

If page takes time to load, on that time, display the time it took to load a page. Following is a set of simple PHP code which would help you achieve this goal.
Place the following snippet at the very top of your page (Try to have header and footer as separate files).

<?php
$timer_start = str_replace(” “,”",microtime());
?>

Then place the following code at the very end of your footer.

<?php
$timer_end = str_replace(” “,”",microtime());
$timer_diff = number_format($timer_end-$timer_start,6,’.',”);
if($timer_diff<0) $timer_diff = “0.00″;echo “<div id=’load-time’ style=’font-family: verdana; font-size: 8pt; color: #444444; text-align: center’>Page load time:<b>$timer_diff</b> second</div>”;
?>

You can leave a response, or trackback from your own site.

2 Responses to “Displaying page loading time dynamically”

  1. Rajesh says:

    Hi vijay,
    Change this line, u missed one double quotes on line $timer_diff = number_format($timer_end-$timer_start,6,’.’,”");. This is error, Now it will work good.

    • Vijay Kumar says:

      Dear Rajesh,
      Thanks for your correction. These type of bugs will not appear once again.

      <?php
      $timer_end = str_replace(” “,”",microtime());
      $timer_diff = number_format($timer_end-$timer_start,6,’.',"");
      if($timer_diff<0) $timer_diff = “0.00″;echo “Page load time:$timer_diff second”;
      ?>

Leave a Reply