Saturday, June 12, 2010

Find out the page loading time using php

If you need to know how long your site takes to load, we can use php script. Using the php microtime() we can find the loading time of a webpage. If the webpage loading time is high we can check the page using this code that where it takes too long time to load.

insert the following code at the top of the PHP page

<?php
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
?>

also put the following code at the bottom of the page

<?php
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime,10);
echo "Time To Load: ".$totaltime;
?>

Now when you access your webpage it will show the time taken to load.

For example :

Time To Load: 0.015601