Start and stop a timer PHP

Start and stop a timer PHP

You can use microtime and calculate the difference:

$time_pre = microtime(true);
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;

Heres the PHP docs for microtime:

Since PHP 7.3 the hrtime function should be used for instrumentation.

$start = hrtime(true);
// run your code...
$end = hrtime(true);   

echo ($end - $start);                // Nanoseconds
echo ($end - $start) / 1000000;      // Milliseconds
echo ($end - $start) / 1000000000;   // Seconds

The mentioned microtime function relies on the system clock. Which can be modified e.g. by the ntpd program on ubuntu or just the sysadmin.

Start and stop a timer PHP

Use the microtime function. The documentation includes example code.

Leave a Reply

Your email address will not be published.