How can you display the approximate remaining runtime of a PHP script with multiple cURL requests on the screen?

When dealing with a PHP script that includes multiple cURL requests, it can be useful to display an approximate remaining runtime on the screen to provide users with an idea of how long the script will take to complete. This can help manage user expectations and provide transparency. To achieve this, you can calculate the average time taken for each cURL request and then estimate the remaining time based on the number of requests left to process.

<?php

// Set the total number of cURL requests to be made
$totalRequests = 10;

// Set the average time taken for each cURL request
$averageRequestTime = 2; // in seconds

// Calculate the total estimated runtime
$totalRuntime = $totalRequests * $averageRequestTime;

// Display the approximate remaining runtime
echo "Approximate remaining runtime: " . gmdate("H:i:s", $totalRuntime) . "\n";

// Perform cURL requests
for ($i = 0; $i < $totalRequests; $i++) {
    // Make cURL request here
    // Update progress and remaining time
    $remainingRequests = $totalRequests - $i - 1;
    $remainingTime = $remainingRequests * $averageRequestTime;
    echo "Processing request " . ($i + 1) . ", Approximate remaining time: " . gmdate("H:i:s", $remainingTime) . "\n";
}

?>