Getting the monotonic time
Get monotonic time: hrtime()
When you need to calculate the elapsed time between two points you will need to save the start time and the end time to calculate the difference. The elapsed time should not be affected by external behavior like time modification by an external software (NTP) or time adjustment.
The hrtime()
returns an array of two integers:
- seconds (first element at index
0
) - nanoseconds (second element at index
1
)
The hrtime()
function may also have an optional second boolean parameter $as_number
. If the parameter is set to true
, then the hrtime()
function returns an int
value (nanoseconds) instead of the array of integers in the form [seconds, nanoseconds]. Probably calling hrtime(true)
makes the calculation of elapsed time easier like in the example below: