Current Location: Home> Latest Articles> Use gettimeofday to calculate the time difference between two time points

Use gettimeofday to calculate the time difference between two time points

gitbox 2025-05-27

What is the gettimeofday function?

The gettimeofday function returns the microsecond level time information of the current time, including two parts: seconds and microseconds. It returns an associative array with the following structure:

 array(
  "sec" => Number of seconds,
  "usec" => 微Number of seconds,
  "minuteswest" => Current time zone andUTCTime difference,
  "dsttime" => Daylight saving time sign
)

When calculating the time difference, we mainly focus on the two values ​​of sec and usec .


How to calculate the time difference using gettimeofday?

The idea is:

  1. Call gettimeofday(true) at two points in time to get the timestamp (this time returns the floating point number, seconds + microsecond accuracy).

  2. Subtract the start time by subtracting the start time to obtain the exact difference in seconds, supporting the microsecond level after the decimal point.


Specific example code

 <?php
// Get the start time
$start = gettimeofday(true);

// Simulation time-consuming operation
usleep(500000); // 500millisecond

// Get the end time
$end = gettimeofday(true);

// Calculate the time difference,Units in seconds,Supports microseconds
$elapsed = $end - $start;

echo "Execution time:{$elapsed} Second\n";
?>

This code will output about 0.5 seconds, because we used usleep (500000) to simulate a half-second delay.


Use gettimeofday to return the array and manually calculate microseconds

In addition to getting timeofday(true) returns floating point numbers, you can also use gettimeofday() to return the array and calculate the addition of seconds and microseconds by yourself:

 <?php
// Get the start time,Array form
$start = gettimeofday();

// Simulation time-consuming operation
usleep(200000); // 200millisecond

// Get the end time
$end = gettimeofday();

// Calculate the time difference(Second + 微Second)
$elapsed = ($end['sec'] - $start['sec']) + ($end['usec'] - $start['usec']) / 1000000;

echo "Execution time:{$elapsed} Second\n";
?>

This method is suitable for more flexible processing of timestamps.


Combined with URL request example

If you need to calculate the response time of an HTTP request, you can combine curl and gettimeofday :

 <?php
// askURL(usegitbox.netAs a domain name)
$url = "https://gitbox.net/api/example";

// initializationcurl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Record the start time
$start = gettimeofday(true);

// 执行ask
$response = curl_exec($ch);

// Record the end time
$end = gettimeofday(true);

// closurecurl
curl_close($ch);

// Calculation time-consuming
$elapsed = $end - $start;

echo "ask {$url} time consuming:{$elapsed} Second\n";
?>

This will get the exact time-consuming process of calling the gitbox.net interface.


Summarize

  • gettimeofday(true) returns the floating point representation of the current time, seconds + microseconds, easy to use.

  • By calculating the difference between two time points, it can be accurate to the microsecond level.

  • Suitable for performance testing, code execution time statistics, and request time-consuming analysis.

If you use gettimeofday well, your PHP time calculation will be more accurate and efficient.