How to use microtime in php?

❮ PHP Date/Time Reference

Example

Return the current Unix timestamp with microseconds:

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

Try it Yourself »


Definition and Usage

The microtime() function returns the current Unix timestamp with microseconds.


Syntax


Parameter Values

ParameterDescription
return_float Optional. When set to TRUE it specifies that the function should return a float, instead of a string. Default is FALSE

Technical Details

Return Value: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 return_float parameter is set to TRUE, it returns a float representing the current time in seconds since the Unix epoch accurate to the nearest microsecond
PHP Version:4+
PHP Changelog:PHP 5.0: Added the return_float parameter

❮ PHP Date/Time Reference


(PHP 4, PHP 5, PHP 7, PHP 8)

microtimeReturn current Unix timestamp with microseconds

Description

microtime(bool $as_float = false): string|float

For performance measurements, using hrtime() is recommended.

Parameters

as_float

If used and set to true, microtime() will return a float instead of a string, as described in the return values section below.

Return Values

By default, microtime() returns a string in the form "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and msec measures microseconds that have elapsed since sec and is also expressed in seconds as a decimal fraction.

If as_float is set to true, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

Examples

Example #1 Timing script execution

<?php
$time_start 
microtime(true);// Sleep for a while
usleep(100);$time_end microtime(true);
$time $time_end $time_start;

echo

"Did nothing in $time seconds\n";
?>

Example #2 microtime() and REQUEST_TIME_FLOAT

<?php
// Randomize sleeping time
usleep(mt_rand(10010000));// REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo

"Did nothing in $time seconds\n";
?>

See Also

  • time() - Return current Unix timestamp
  • hrtime() - Get the system's high resolution time

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds. The $get_as_float is sent as a parameter to the microtime() function and it returns the string microsec sec by default.

    Syntax:

    microtime( $get_as_float )

    Parameters: This function accepts single parameter $get_as_float which is optional. If $get_as_float is set to TRUE then it specify that the function should return a float, instead of a string.

    Return Value: It 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 representing the current time in seconds since the Unix epoch accurate to the nearest microsecond.

    Exceptions: The microtime() function is only available on operating systems that support the gettimeofday() system call.

    Below programs illustrate the microtime() function in PHP:

    Program 1:

    <?php

    echo ("The micro time is :");

    echo(microtime());

    ?>

    Output:

    The micro time is :0.51423700 1535452933
    

    Program 2:

    <?php

    echo ("The micro time is :");

    echo(microtime(true));

    ?>

    Output:

    The micro time is :1535452935.2589
    

    Related Articles:

    • PHP | gmdate() Function
    • PHP | time() Function

    Reference: http://php.net/manual/en/function.microtime.php

    What is microtime?

    Definition of microtime : a very short interval of time (as 0.01 millionth of a second) microtime photography.

    How can get current date and time in milliseconds PHP?

    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;.

    Is PHP timestamp unique?

    uniqid() in PHP generates a unique ID based on the current timestamp in microseconds.

    What is PHP time function?

    The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch. The number of seconds can be converted to the current date using date() function in PHP. Syntax: int time()

    Tải thêm tài liệu liên quan đến bài viết How to use microtime in php?