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] => 1728150662
[usec] => 735741
[minuteswest] => 0
[dsttime] => 0
)
The first element [sec] gives the time in seconds elapsed between Unix Epoch time and current time. echo gettimeofday(true);
The output is here. 1728150662.7358
Refresh this page and see how the data is changing 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: 1728150662
Microseconds: 735787
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.
2024-10-05 17:51:02
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";