Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Calculate Page Load Time in PHP

Sometimes we need to Calculate how long does it take to Load a web page which we create from code.

Website loading speed is very much important both from user and search engine point of view.

If a website takes too much loading time then users don’t wait for it. So by checking how much time your website is taking to load, you can optimize the speed and performance.

So, In this tutorial, I am going to show you how you can calculate page load time in PHP. To calculate page load time we have to create a function using microtime inbuilt function.

Here, “microtime” gives the current Unix timestamp with microseconds.

Create a User Defined Function

Create a user defined function to get the current time.

function getTime()
{
 return array_sum(explode(" ",microtime()));  
}

$startTime = getTime();

// All your code
for ($i=0; $i}

$endTime = getTime();

$timeTaken = round(($endTime - $startTime),2);

echo "Page loaded in $timeTaken seconds";
?>

We have to use this function two times on a webpage, one at the beginning and another at the end of the webpage.

We are using two variables to store start time and end time respectively. In the middle, we write all code for functionality.

In the end, subtracting the start time from the end time to get the time taken to load a web page.

Let’s see how to use this script in PHP file to calculate page load time.

Create a PHP file and define markup and scripting.

Page Load Time PHP - PhpCluster

Page Load Time in PHP

This is sample paragraph

Conclusion

In this tutorial, you have learned how to show page loading time using PHP.

The post Calculate Page Load Time in PHP appeared first on PhpCluster.



This post first appeared on Best PHP Programming Blog, Blog For PHP Developers, PHP Tutorial,PHP Blog For Beginners, please read the originial post: here

Share the post

Calculate Page Load Time in PHP

×

Subscribe to Best Php Programming Blog, Blog For Php Developers, Php Tutorial,php Blog For Beginners

Get updates delivered right to your inbox!

Thank you for your subscription

×