Displaying your page load time

Browse: Tutorials » PHP
The first step is to place the following code at the very top of your php file to start the timer, so we can display the load time.

<?php

// Insert at the very top of your page

$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;

?>

Then at the very bottom of your page you need to add this code to stop the timer and then display the total time.

<?php

// Place at the very bottom of your page

$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("Page took %f seconds to load.", $totaltime);
// The above line can be changed but remember to keep %f

?>

The script currently outputs

Page took 0.15235 seconds to load.

But you can change the text to anything you like by changing the text in the quotes, the time is output where the %f is placed.

printf ("Page took %f seconds to load.", $totaltime);


Hope this is helpful!