- Bohack - https://www.bohack.com -

PHP Page Stop Watch

Lots of time I have web pages performing complex functions and want to make sure that they execute without a problem. A problem would be they just take too damn long and time out when put under a load. This is the problem when things don’t scale properly, they just get really slow. So in order to gauge how long a page takes to load we really need a stop watch. Luckily we the components of one…

The PHP microtime() function returns the UNIX timestamp with micro-seconds.

<?php echo (microtime()) ?>;   //returns 0.123123 1231231231

The first number is the microseconds from the change of the second number, which is the time in seconds from the UNIX epoch of 0:00:00 Jan 1st 1970. So we can explode the line returned into an array using the explode command, then mathematically add them together. i.e. 1231231231.123123. The last line will take the end time and subtract the start time. The result will be displayed in the HTML comments towards the end of the page, where this code is executed.

Put this at the beginning of your page inside of the php tags.

$start=explode(" ",$start);

// do something here

Put this at the end of you page inside of the php tags.

$end=explode(" ",$end);

printf("\n<!-- Page was generated by PHP %s in %f seconds -->\n",phpversion(),$end-$start);