PHP Snippet - Script Runtime

Posted at

- REQUEST_TIME_FLOAT is a timestamp of the start of the request, with microsecond precision (PHP 5.4.0+).
- microtime(true) returns a float of the current Unix timestamp (microseconds precision).

<?php
$time_start = call_user_func(function(){
                $result = filter_input(INPUT_SERVER, 'REQUEST_TIME_FLOAT', FILTER_SANITIZE_NUMBER_FLOAT);
                return false === $result || null === $result ? microtime(true) : $result;
              });

echo microtime(true) - $time_start
?>


- This is a "dictionary code", in-order to save the extra anonymous-function call and checking if REQUEST_TIME_FLOAT is available, just use microtime all-along :)

Best practice:
Always use filter_input with some sort of filtering, try to avoid accessing the global variables directly ($_SERVER),
It will also make most of the code-validation engines happy ;)