Getting the current UNIX timestamp
Return current UNIX timestamp: microtime()
The function microtime()
returns the current UNIX timestamp in seconds, with microseconds.
The function microtime()
may have an input boolean parameter to control the output:
- no input or input
false
: it returns a string in the form βmsec secβ;
- input
true
: it returns a float sec.msec.
Why you need to avoid to use microtime() to measure the time
If you need to measure the time consumed, like a stopwatch or timer, you should use the hrtime()
function.
This is because microtime()
reads the current time from the clock.
If you read the start time and the end time to calculate the difference you could see some inconsistency. For example, letβs try to think about if the datetime is changed between readings (manually or software like ntp will update the clock, so the date time).