Ping host measured in microseconds


You want to ping a host on a given port and record time taken in microseconds.

The following solution uses PHP socket libraries to open a connection, to the given host and port – then just close it again. By obtaining the microseconds before and after, it is able to deduce time taken in microseconds.

Additionally I’ve added functionality to print the epoch, so the timestamp can be obtained.




list($usec, $sec)=explode(" ",microtime());
$thisDate=sprintf("%f", ($usec+$sec));

if(!$host) {
die("Usage: $0 host port [ dtg ]n");

$fp = fsockopen("$host", $port, $errno, $errstr, 30);
if (!$fp) {

print("ping failed to host: $host:$port -
$errstr ($errno)n");

} else {

list($usec, $sec)=explode(" ",microtime());
$nextDate=sprintf("%f", ($usec+$sec));

if($argv[3]) {
} else {






First example pings a host on port 80 and prints the current epoch.

$ php -q ./pingStat.php apachehost 80 y

Second example pings it again on port 443 and this time does not print epoch.

$ php -q ./pingStat.php apachehost 443



[tags]Ping host measured in microseconds, PHP Coding School[/tags]


If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *