PHP microtime() Function

What is PHP microtime() Function?

If you want to measure the current time as a number of seconds with microseconds, use PHP microtime() function. An example is “0.11232600 1704246151”. Here, the second part is the number of seconds. And the first part is the microseconds elapsed since the first part, seconds and it is expressed in seconds as a decimal fraction.

The function calculates time from the 1st January 1970 00:00:00 GMT (which is known as UNIX Epoch) to the current time. This duration is known as timestamp of the current time. You can separate the microseconds from the output. Check example 4. And, you can convert the second part to human readable date and time format later.

Syntax:

microtime(return_as_float)

Parameters:

This function has only one optional parameter-

return_as_float (Optional): If you mention TRUE as the parameter, the function returns its value as a float instead of a string.

Return Values:

Depending on the parameter’s Boolean value, the function returns two type of output-

  • If you use FALSE (default) or omit the parameter, the function returns the timestamp in “microsecond second” format. As you see, it has 2 parts separated by a space. So, the function returns the microsecond in the first part and the seconds in the last part. Check example 1.
  • If you use TRUE, it returns the timestamp as a float. In the float value, before the decimal point, it indicates the seconds and after the decimal point, it indicates microsecond. Check example 2.

Examples:

Example 1: Measuring time without parameter or using FALSE as parameter in microtime() function-

<?php
$timestamp = microtime();
echo var_dump($timestamp);
echo "<br >";
$timestamp = microtime(FALSE);
echo var_dump($timestamp);
?>

Output:

string(21) “0.57022700 1704253681”
string(21) “0.57024600 1704253681”

Explanation:

The microtime() function returns same result with FALSE as parameter or without any parameter. And it returns result as a string.

Example 2: Measuring time using TRUE as parameter in microtime() function-

<?php
$timestamp = microtime(TRUE);
echo var_dump($timestamp);
?>

Output:

float(1704253681.57025)

Explanation:

The TRUE parameter returns the timestamp as a float.

Example 3: Separating seconds and microseconds in microtime() function-

<?php
echo "Current timestamp is ";
$timestamp = microtime();
$parts = explode(" ", $timestamp);
echo $parts[1] . " seconds and ";
echo $parts[0] . " seconds.";
echo "<br />";
echo date("m/j/Y g:i:s a", $parts[1]);
?>

Output:

Current timestamp is 1704254182 seconds and 0.65432000 seconds.
It is same as 01/2/2024 10:56:22 pm

Explanation:

We use explode function to split the string by the space and get second and microsecond parts. Note, the first part $parts[0] (line 6) is expressed in second as a decimal fraction.

Example 4: Calculating script’s execution time with microtime() function-

<?php
$timestamp1 = microtime(TRUE);
str_pad("Hello", 1000," ");
sleep(1);
/* Other codes
goes here.
*/
$timestamp2 = microtime(TRUE);
echo "This script took " . ($timestamp2 - $timestamp1) . " seconds to execute.";
?>

Output:

This script took 1.0004839897156 seconds to execute.

Explanation:

Before starting writing any code, we get the timestamp with microtime() function. And, the last piece of code of the script, the microtime() function tells us the timestamp of that moment. Subtracting the two, we can get to know the script execution time.

Example 5: Measuring code blocks execution time with microtime() function-

<?php
$start1 = microtime(TRUE);
// Start: Code block 1
sleep(1);
// Codes go here....
// End: Code block 1
$start2 = microtime(TRUE);

// Start: Code block 2
sleep(1);
// Codes go here....
// End: Code block 2
$start3 = microtime(TRUE);

// Start: Code block 3
sleep(1);
// Codes go here....
// End: Code block 3
$start4 = microtime(TRUE);

echo "Code block 1 took " . ($start2 - $start1) . " seconds to execute. <br />";
echo "Code block 2 took " . ($start3 - $start2) . " seconds to execute. <br />";
echo "Code block 3 took " . ($start4 - $start3) . " seconds to execute.";
?>

Output:

Code block 1 took 1.0022211074829 seconds to execute.
Code block 2 took 1.0023899078369 seconds to execute.
Code block 3 took 1.0024690628052 seconds to execute.

Explanation:

Before and after of each block, we set two microtime() functions. Subtracting of these two timestamps we can measure each blocks execution time and find out which is causing issue.

Example 6: Measuring database querying time with microtime() function-

<?php
$start = microtime(TRUE);
$result = mysqli_query($connection, "SELECT * FROM project ORDER BY ProID");
$end = microtime(TRUE);
echo "The query took " . ($end - $start) . " seconds";
?>

Explanation:

We keep track of 2 timestamps – one is just before starting the query and the other is just after completing the query. Subtracting the two times, we get the query execution time in seconds and miliseconds.

Practical Usages of microtime() Function:

Few of this function’s usages include-

  • Script execution time in web application is a vital issue. You can measure a script execution time with this function. Check example 4.
  • When a script takes long time, you need to find out which part of the code takes the time so that you can optimization the code. You can divide codes in blocks and measure execution time with tis microtime() function. Check example 5.
  • When your database begins to increase with data, some of your queries may take time and slow down your web application. You can use microtime() function to check query execution time. Check example 6.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP microtime() Function

microtime() is a built-in date and time function in PHP that helps you perform time calculation in very precise level.

Reference:

https://www.php.net/manual/en/function.microtime.php