PHP Tutorial - PHP microtime() Function






Definition

The microtime() function returns current time. When called without any parameters, this returns the current system time in seconds and microseconds, ordered microseconds first. For example: 0.12342123 1234123412.

Syntax

mixed microtime ( [bool float_output ] )

Parameter

PHP microtime() Function has the following parameter.

  • float_output - Optional. TRUE specifies that the function should return a float, instead of a string. Default is FALSE




Return

Returns the string "microsec sec" by default, where sec is the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and microsec is the microseconds part.

If the get_as_float parameter is set to TRUE, it returns a float to the nearest microsecond representing the current time in seconds since the Unix epoch.

Example

Return the current Unix timestamp with microseconds:


<?php
echo(microtime());
?>

The code above generates the following result.





Example 2

The following code shows how to use current Unix timestamp with microseconds in for loop.


<?php// w  w  w  .  j  ava  2s . com

$startTime = microtime( true );

for ( $num = 1; microtime( true ) < $startTime + 0.0001; $num = $num * 2 ) {
  echo "Current number: $num<br />";
}

echo "Out of time!";

?>

The code above generates the following result.