What is PHP gettimeofday() Function?
If you want to get the current time with second even microsecond level precision, use PHP gettimeofday() function.
Here, the time is calculated from the Unix epoch(00:00:00 UTC on January 1, 1970) to the moment the function runs.
Syntax:
gettimeofday(return_float)
Parameters:
The Function has 1 parameter which is optional –
return_float (Optional): It specifies a Boolean value (TRUE or FALSE).
Return Values:
Depending on the parameter (the boolean), the function returns different result-
- FALSE – It is the default value. When it is FALSE, the function returns an associative array which contains time from Unix Epoch in second and microsecond. The associative array has the following keys-
1) sec – Seconds since the Unix Epoch.
2) usec – Microseconds
3) minuteswest – Minutes west of Greenwich
4) dsttime – Type of DST (Daylight Saving Time) correction.
Check example 1. - TRUE – When it is TRUE, the function returns a float which represents seconds with microsecond. Check example 2.
Examples:
Example:
<pre>
<?php
$current_time = gettimeofday();
print_r($current_time);
echo "The Seconds of current time since Unix Epoch is: " . $current_time['sec'] . "<br/>";
echo "and Microseconds: " . $current_time['usec'];
?>
</pre>
Output:
Array
(
[sec] => 1768106761
[usec] => 360018
[minuteswest] => -60
[dsttime] => 0
)
The Seconds of current time since Unix Epoch is: 1768106761
and Microseconds: 360018
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP gettimeofday() Function
The gettimeofday() function may be the most commonly used built-in PHP date and time function. use this function when you when you need time in microsecond precision.