php measure time code example

Example 1: php milliseconds

//Timing executation time of script
$startTime = microtime(true); //get time in micro seconds(1 millionth)
usleep(250); 
$endTime = microtime(true);

echo "milliseconds to execute:". ($endTime-$startTime)*1000;

Example 2: php check how much time each instruction takes to complete

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;

Example 3: time now with milliseconds php

$d = new DateTime();
echo $d->format("Y-m-d H:i:s.v"); // v : Milliseconds

Tags:

Php Example