|
Loading time of a page |
How many of you who have websites have wondered how long it takes to load a page on the site.
The loading time is a very important for a webmaster because of the lightness of an index page and a page loads more quickly, more is better navigation.
After this introduction, let's see how you can measure the load time of a web page with php.
|
The function get_time |
<?php
function get_time()
{
// Detect the time
$time_now = microtime();
// Separates seconds and milliseconds in array
$array_time = explode(" ",$time_now);
// We put together seconds and milliseconds
$time_return = floatval($array_time[1]) + floatval($array_time[0]);
return $time_return;
}
?>
|
I remarks quickly the four lines of code contained by the function that I built:
- The function microtime() returns a string containing the microseconds and seconds (separated by a space) that have passed since 1 January 1970
- The $array_time separate the second from milliseconds
- The floatval transforms into numeric seconds and milliseconds, and adds them to have the total elapsed time in seconds since 1 January 1970.
- The function returns the time passed since 1 January 1970
|
Time detection |
After the tag <body> we put the first time detection: |
<?php
$time1 = get_time();
?>
|
Before the tag </body> we put the second time detection:
|
<?php
$time2 = get_time();
?>
|
Distance between time detections |
<?php
$difference = abs($time2 - $time1);
?>
|
The loading time of the page is in the variable $difference. |