Skip to content
PHP Logo

How can client and server time synchronization affect the accuracy of measuring request duration in PHP?

Client and server time synchronization is crucial for accurately measuring request duration in PHP. If the client and server times are not synchronized, the calculated duration may be inaccurate due to time discrepancies. To solve this issue, you can use the server's time as a reference point for measuring request duration by capturing the server's timestamp when the request is received and comparing it to the client's timestamp when the response is sent.

php
// Capture server timestamp when request is received
$server_start_time = microtime(true);

// Perform some operations

// Capture client timestamp when response is sent
$client_end_time = microtime(true);

// Calculate request duration using server time as reference point
$request_duration = $client_end_time - $server_start_time;

echo "Request duration: " . $request_duration . " seconds";