Mostrando el uptime con PHP
Sábado, 28 de abril de 2007
Hasta ahora usaba un archivo php y con un iframe mostraba el uptime en el footer de wordpress. Ahora he modificado el theme y tengo puesto el uptime del servidor, de forma estática, en el header -> Bienvenido a TheDavis Blog, un sito un tanto especial… @ 6 days, 5 hours, 15 minutes, 40 seconds. Para realizar esto aquí dejo la función en PHP, esto funciona solo si tenemos el servidor web bajo Linux:
function format_uptime($seconds) {$secs = intval($seconds % 60);$mins = intval($seconds / 60 % 60);$hours = intval($seconds / 3600 % 24); $days = intval($seconds / 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"); } return $uptimeString; } // read in the uptime (using exec) $uptime = exec("cat /proc/uptime"); $uptime = split(" ",$uptime); $uptimeSecs = $uptime[0]; // get the static uptime $staticUptime = "Uptime: ".format_uptime($uptimeSecs); ?>
Para mostrar el string hay que escribir lo siguiente allí donde queramos:
<?php echo $staticUptime; ?>
También se puede usar una funcion en javascript para leer el parametro $uptimeString y actualizar el uptime cada cierto tiempo, 10 segundos, 1 segundo por ejemplo.







