PHP Uptime Function


Written by

There is an updated version of this function.

I have been wanting to display the uptime of my Web Server so I wrote a little function.

It only supports Linux/Unix type systems but it checks for that and if its a Windows or Unknown system, it just outputs “Unknown”.

function uptime(){
 // See if it is a Unix Based OS
 if((PHP_OS == "Linux") || (PHP_OS == "FreeBSD") || (PHP_OS == "Unix") || (PHP_OS == "SunOS")){
  $uptime = file_get_contents( "/proc/uptime");
  $uptime = explode(" ",$uptime);
   $uptime_ = $uptime[0];
  $seconds = $uptime;
 
  $days = (($uptime % 31556926) / 86400);
  $days = explode(".",$days);
   $days = $days[0];
  $hours = ((($uptime % 31556926) % 86400) / 3600);
  $hours = explode(".",$hours);
   $hours = $hours[0];
  $minutes = (((($uptime % 31556926) % 86400) % 3600) / 60);
  $minutes = explode(".",$minutes);
   $minutes = $minutes[0];
  if($minutes > 0){
   $time = $minutes." mins.";
  }
  if($hours > 0){
   $time = $hours." hours, ".$time;
  }
  if($days > 0){
   $time = $days." days, ".$time;
  }
 
 }else{
  // Function Doesn't Support Windows
  $time = "Unknwown";
 }
return $time;
}

Hope someone else gets as much use of it as me.