So who remembers back in the day when one of the coolest things you could have on your website’s footer was the page load time? As passé as that may seem in this day in age, it’s still something that can be useful when attempting to profile a site or track down bottlenecks in your code. Calculating the time it takes is as simple as storing the start time in a variable at the top of your script and subtracting it from the end time at the bottom of the script:

<?php
$started_at = microtime(true);

// This is where your logic will live
// echo 'Lorem ipsum dolor sit amet...';
// et cetera.

echo 'Cool, that only took ' . (microtime(true) - $started_at) . ' seconds!';
?>

Obviously without any logic in there it will run pretty darn quick. Feel free to slap a sleep() or usleep() to delay the execution as bit.

As of PHP 5.4 there is a new $_SERVER variable called REQUEST_TIME_FLOAT which eliminates the need to capture the start time. Just put this line at the end of your file:

echo 'Page loaded in ' . microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'] . ' seconds!';

Please note that if you are not using PHP 5.0+ microtime() will not accept the argument to force it to return a float instead of a string. To get around this, you will need to explode the return of microtome() and add the 2 resulting numbers together:

$microtime  = explode(' ', microtime());
$started_at = (float)$microtime[0] + (float)$microtime[1];

As always, I recommend upgrading PHP if you’re not yet using PHP 5.0.0 or newer ;)