I found this on the web and it’s pretty clever – since, unlike Unix, Windows doesn’t have a standard “uptime” function to display the time a server’s been running. There are a couple of issues, though. The original code was missing a line (line #15) which threw a non-fatal error. In other words – following a tip from another web page – I added the line
1 |
$uptimeString=""; |
And here’s something very odd: when placed in a web page – apart from WordPress – this thing works perfectly. If used as an include, or, in conjunction with a PHP code plugin in WordPress it adds 5 hours to the total uptime. Of course, I have no idea why this is the case……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php // format the uptime in case the browser doesn't support dhtml/javascript // static uptime string function format_uptime($seconds) { $secs = intval($seconds % 60); $mins = intval($seconds / 60 % 60); $hours = intval($seconds / 3600 % 24); $days = intval($seconds / 86400); $uptimeString=""; if ($days > 0) { $uptimeString .= $days; $uptimeString .= (($days == 1) ? " day" : " days"); } if ($hours > 0) { $uptimeString .= (($days > 0) ? ", " : "") . $hours; $uptimeString .= (($hours == 1) ? " hour" : " hours"); } if ($mins > 0) { $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins; $uptimeString .= (($mins == 1) ? " minute" : " minutes"); } if ($secs > 0) { $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs; $uptimeString .= (($secs == 1) ? " second" : " seconds"); } return $uptimeString; } // get the server statistics with "net statistics server" by shell_exec $winstats = shell_exec("net statistics server"); // grab the date & time the server started up preg_match("(\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}\:\d{2}\s+\w{2})", $winstats, $matches); // convert the readable date & time to a timestamp and deduct it from the current timestamp // thus giving us the total uptime in seconds $uptimeSecs = time() - strtotime($matches[0]); // get the static uptime $staticUptime = "Server Uptime: ".format_uptime($uptimeSecs); ?> <html> <head> <script language="javascript"> <!-- var upSeconds=<?php echo $uptimeSecs; ?>; function doUptime() { var uptimeString = "Server Uptime: "; var secs = parseInt(upSeconds % 60); var mins = parseInt(upSeconds / 60 % 60); var hours = parseInt(upSeconds / 3600 % 24); var days = parseInt(upSeconds / 86400); if (days > 0) { uptimeString += days; uptimeString += ((days == 1) ? " day" : " days"); } if (hours > 0) { uptimeString += ((days > 0) ? ", " : "") + hours; uptimeString += ((hours == 1) ? " hour" : " hours"); } if (mins > 0) { uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins; uptimeString += ((mins == 1) ? " minute" : " minutes"); } if (secs > 0) { uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") + secs; uptimeString += ((secs == 1) ? " second" : " seconds"); } var span_el = document.getElementById("uptime"); var replaceWith = document.createTextNode(uptimeString); span_el.replaceChild(replaceWith, span_el.childNodes[0]); upSeconds++; setTimeout("doUptime()",1000); } // --> </script> </head> <body onLoad="doUptime();"> <!-- Uses the DIV tag, but SPAN can be used as well --> <div id="uptime" style="font-weight:bold;"><?php echo $staticUptime; ?></div> </body> </html> |
For Linux
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php ########################################################################### # # # Copyright © http://www.4webhelp.net/ # # Neither http://www.4webhelp.net/ nor its members accept any # # responsibility, either expressed or implied, for any damage caused by # # using this script or the misuse of this script. # # # # # # INSTRUCTIONS # # # # 1) Copy this code to an editor such as Notepad and save it with a # # .php extension. # # 2) FTP this file to a folder on your site in ASCII mode # # 3) Call up this file in your web browser to see your server's uptime # # # ########################################################################### $data = shell_exec('uptime'); $uptime = explode(' up ', $data); $uptime = explode(',', $uptime[1]); $uptime = $uptime[0].', '.$uptime[1]; echo ('Server Stats:<br />Uptime: '.$uptime.' '); ?> |