Friday, August 14, 2009

PHP Server Uptime

This is a pretty simple way of getting the server uptime using PHP. Note that this only works on Linux (and probably other Unix-like OSes that store the machine uptime in /proc/uptime). Straight to the code: function get_uptime() { $file = @fopen('/proc/uptime', 'r'); if (!$file) return 'Opening of /proc/uptime failed!'; $data = @fread($file, 128); if ($data === false) return 'fread() failed on /proc/uptime!'; $upsecs = (int)substr($data, 0, strpos($data, ' ')); $uptime = Array ( 'days' => floor($data/60/60/24), 'hours' => $data/60/60%24, 'minutes' => $data/60%60, 'seconds' => $data%60 ); return $uptime; }

2 comments: