What are the best practices for determining and comparing timestamps between client and server in PHP for measuring request duration?

When measuring request duration between client and server in PHP, it's important to ensure accurate timestamp comparisons by using a consistent time format and accounting for any time differences between the client and server. One common approach is to use Unix timestamps, which represent the number of seconds since the Unix epoch (January 1, 1970). By converting both client and server timestamps to Unix timestamps, you can easily calculate the duration of the request.

// Client timestamp
$clientTimestamp = $_SERVER['REQUEST_TIME_FLOAT'];

// Server timestamp
$serverTimestamp = microtime(true);

// Convert timestamps to Unix timestamps
$clientUnixTimestamp = strtotime(date("Y-m-d H:i:s", $clientTimestamp));
$serverUnixTimestamp = strtotime(date("Y-m-d H:i:s", $serverTimestamp));

// Calculate request duration
$requestDuration = $serverUnixTimestamp - $clientUnixTimestamp;

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