Web Hosting Talk







View Full Version : Uptime Reporting in PHP


NetSender
06-10-2004, 04:18 PM
Was just wondering if there is anyway of returning the system uptime when using php under a windows environment. I know in linux it can easily be implemented using :

echo shell_exec(uptime);


but I do not know the windows equivalent, can anyone help ??

Thanks

Matt

SeņorAmor
06-10-2004, 04:30 PM
Start > Run > cmd > 'net statistics workstation' brings up a lot of useful info including when your computer was last restarted. That might help.

CrZyAsMHaCkeR
06-10-2004, 07:51 PM
This script should fetch the uptime in seconds from the Windows command the above user posted:


<?php

$stats = shell_exec ('net statistics workstation');

if (preg_match ('/Statistics since (\\d{1,2})\\/(\\d{1,2})\\/(\\d{4}) (\\d{1,2}):\(\\d{2}) (A|P)M/', $stats, $matches)) {

$up = mktime ($matches[6] == 'A' ? ($matches[4] == 12 ? 0 : $matches[4]) : ($matches[4] == 12 ? 12 : $matches[4] + 12),
$matches[5][0] == '0' ? $matches[5][1] : $matches[5], 0, $matches[1],
$matches[2][0] == '0' ? $matches[2][1] : $matches[2], $matches[3]);

$uptime = time () - $up;
echo 'Uptime: ' . $uptime . ' seconds';

} else {

echo 'Could not be determined.';

}

?>


EDIT: I had to edit this a few times because somehow this bulletin board got the idea I wanted an <img> tag in my regular expression. It works okay now.