gettimeofday(): the current time as an associative array or a floating-point number

Get the current time with microsecond data

gettimeofday() returns current time information, including seconds and microseconds. Passing true returns a float similar to microtime(true).

$time = gettimeofday();
echo $time['sec'] . "
";
echo $time['usec'];

For elapsed-duration measurement inside one request, a monotonic timer such as hrtime() is usually a better fit than wall-clock time.

In PHP by using gettimeofday function we can get the time elapsed between current time and Unix Epoch time.

We get an array in return.
Here is the syntax
gettimeofday();
We will try to generate output by using this. Here is the sample code
print_r(gettimeofday());
The output of above code is here
Array
(
    [sec] => 1790736893
    [usec] => 423746
    [minuteswest] => 0
    [dsttime] => 0
)
The first element [sec] gives the time in seconds elapsed between Unix Epoch time and current time.
The second element [usec] gives in microseconds.
[minuteswest] - minutes west of Greenwich
[dsttime] - type of dst correction
All these data are related to server side and not the local time of the client computer.

Getting float data

Instead of an array we can get float value like this.
echo gettimeofday(true);
The output is here.
1790736893.4238
Refresh this page and see how the data is changing

Example 2: Retrieving Time of Day in Seconds and Microseconds

The gettimeofday() function returns the current time, including seconds and microseconds. Here's how to extract both components:

$time = gettimeofday();
echo "Seconds: " . $time['sec'] . "<br>";
echo "Microseconds: " . $time['usec'];
Output
Seconds: 1790736893
Microseconds: 423767

Example 3: Formatting Time with Date()

You can combine gettimeofday() with date() to format the current time:

$time = gettimeofday();
echo date("Y-m-d H:i:s", $time['sec']);
Output , Refresh this page to see the time updates.
2026-09-30 02:54:53

Example 4: Calculating Script Execution Time

Use gettimeofday() to measure script execution time in seconds and microseconds:

$start = gettimeofday();
usleep(500000); // Delay for demonstration
$end = gettimeofday();

$execution_time = ($end['sec'] - $start['sec']) + ($end['usec'] - $start['usec']) / 1000000;
echo "Execution time: " . $execution_time . " seconds";

PHP Date Functions getTimestamp(): Timestamp from date object


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer