Last Updated:

Find out the time of script execution in PHP

Sometimes you need to know when a script runs. And this can be done using the built-in PHP functions.

Various options can be given. Why might someone want to know the time it takes for the code to run.

This can be as a personal interest in testing the speed of execution of other people's functions, libraries, classes, etc.

So it is when developing your scripts and measuring the speed of execution in order to further improve.

Let's imagine that you have developed some function that is performed for some time and you are not satisfied with its speed of work.

And you gradually improve it. But it's not entirely clear whether these improvements bring any benefit in the speed of execution or not.

To do this, you can measure the speed of its implementation.

Option One 

You can use the dateTime class built into the PHP language to provide the date and times (it began to be supported from PHP 5 to the current version of PHP7).
I will say right away that within the framework of this example, the usleep() function will be used to measure the speed, which will postpone the execution of the program for a specified number of microseconds. You can perform validation on your code, which you want to test for speed of execution.

And so. First, mark the point from which you need to start noticing the time:


Create an instance of the DateTime class with the 'now' parameter to get the current time.

After that, you need to place the body of the script under test (for example, the usleep function).

Now we get the end time of the script:

And get the difference between the beginning and end of the script execution:

And display the result as the number of seconds and milliseconds:
$startTime = new DateTime('now');
usleep(1000000); // Function that delays program execution by 1000000 microseconds
$endTime = new DateTime('now');
$interval = $startTime->diff($endTime);

echo $interval->format('%S seconds, %f microseconds');

For a more detailed and complete result, you can perform a complete printout of the result in the flesh up to the indication of the year.
$interval->format('%Y years, %M months, %D days, %H hours, %I minutes, %S seconds, %f microseconds'');

As a result, we should get the time for which the script will execute.
I have a script execution speed:

01 seconds, 441 microseconds


All the code in this example:


Option Two:
Use the built-in microtime() function, which is supported from PHP4 to the current version of PHP7 at the time of writing.
$startTime = new DateTime('now');
usleep(1000000);
$endTime = new DateTime('now');
$interval = $startTime->diff($endTime);
echo ($interval->format('%S seconds, %f microseconds'));

The microtime() function returns the current Unix timestamp in microseconds. This function is only available on operating systems that have a system call to gettimeofday().

Create a variable that will detect the time of the start of the check:

With a parameter of the microtime function, we pass true in order to get the result in the real form of float, which is the current time in seconds that have elapsed since the beginning of the Unix era with an accuracy of microseconds.

After that, we place the body of the script under test (for example, this is the usleep function).

Now we get the end time of the script:

Find the difference between the beginning and end of the script:

And display the result:
$start = microtime(true);
usleep(1000000); // Function that delays program execution by 1000000 microseconds
$end = microtime(true);
$diff = $end - $start;

echo 'Script execution time: '.($diff).' sec.';

My script ran for:

Script execution time: 1.0004560947418 sec.


I don't know which option works more precisely, but I assume that the option using the microtime() function should be more accurate, since in the above first case, it should take more time to create an instance of the DateTime('now') class.
$start = microtime(true);
usleep(1000000);
$end = microtime(true);
$diff = $end - $start;
echo 'Script execution time: '.($diff).' sec.';