# High-Resolution Monotonic Timer

> Section in “New and Extended Functionality › Date and Time” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/date-and-time/high-resolution-monotonic-timer/>

Available since PHP 4, the `microtime()` function returns the current Unix timestamp with microseconds:

``` php
var_dump(microtime());
var_dump(microtime(true));
```

Executing the code shown above will print the output shown below:

    string(21) "0.10677600 1586956167"
    float(1586956167.1068)

Without the `true` argument, `microtime()` returns a string in the format “microseconds seconds” where the latter is the number of seconds since the Unix epoch, and the former is the number of microseconds since “seconds”.

With the `true` argument, `microtime()` returns a float value that represents the number of seconds since the Unix epoch with microsecond granularity.

The `microtime()` function looks at the system clock to perform its task. Depending on the use case, for instance when you want to measure performance, this may be problematic.

A common use case for `microtime()` is measuring the time between two points in time:

``` php
$start = microtime(true);

// ... do something ...

$end = microtime(true);

$elapsed = $end - $start;
```

The intent of the code shown above is that `$elapsed` contains the time, in microseconds, that passed between the two points in time where `microtime()` was invoked.

If the system clock is changed between the two invocations of `microtime()` then the result in `$elapsed` will be wrong. The `hrtime()` function was introduced in PHP 7.3 to solve this problem.

`hrtime()` implements a so called high-resolution monotonic timer. The “high-resolution” part refers to the fact that the time resolution of `hrtime()` is nanoseconds where `microtime()` only has microsecond resolution. “monotonic” is a reference to the concept of “monotonic functions” in mathematics: a function is monotonic when its values are either entirely non-increasing or entirely non-decreasing.

The values returned by the `hrtime()` function are monotonically increasing, meaning they always increase (or stay the same) and never decrease.

``` php
var_dump(hrtime());
var_dump(hrtime(true));
```

Executing the code shown above will print the output shown below:

    array(2) {
      [0]=>
      int(30955)
      [1]=>
      int(926320146)
    }
    int(30955926404559)

`hrtime()` returns a timestamp at an unspecified point in the past. Without the `true` argument, this timestamp is represented as an array where the first element contains the number of seconds whereas the second element contains the number of nanoseconds.

With the `true` argument, `hrtime()` returns the timestamp as nanoseconds. On a 64-bit system the returned timestamp is an integer, on a 32-bit system it is a float.

`hrtime()` does not depend on the system clock to perform its job. Even if the system clock is changed between the two invocations of `hrtime()` then the result in `$elapsed` will still be correct:

``` php
$start = hrtime(true);

// ... do something ...

$end = hrtime(true);

$elapsed = $end - $start;
```

Furthermore, the value of `$elapsed` is now more accurate which is important when taking performance measurements.

---

Previous: [DateTime::createFromImmutable()](https://php7explained.com/new-and-extended-functionality/date-and-time/datetimecreatefromimmutable/index.html.md) | Next: [MySQL, Prepared Statements, and Fractional Seconds](https://php7explained.com/new-and-extended-functionality/date-and-time/mysql-prepared-statements-and-fractional-seconds/index.html.md)
