PHP Classes
elePHPant
Icontem

File: uptime.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Morgan Christiansson  >  uptime  >  uptime.php  >  Download  
File: uptime.php
Role: ???
Content type: text/plain
Description: Main file
Class: uptime
Author: By
Last change:
Date: 15 years ago
Size: 1,871 bytes
 

Contents

Class file image Download
<?PHP
////////////////////////
// php uptime 1.0
// Morgan Christiansson
// mog@linux.nu
//
// Please mail me if you use it and/or have any comments.
// I might add some month, and week checking too.
//
// Oh, and this is basically just uptime.c ported to php, i just added floor() and $ to the code. :)
// And some formatting stuff
//
// put something like this in your code:
// <?PHP echo uptime("Server up for %days days, %hours hours, %mins minutes and %secs days"); ?>
// or
// <?=time("Server up for %days days, %hours hours, %mins minutes and %secs days")?>
//
// You can also check if days is set, just use this:
// <?PHP
//  if(uptime("%days") == "") {
//   echo(uptime("Up for only %hours");
//  } elseif(uptime("%days") > 30) {
//   echo(uptime("Up for %days days!! wohoo!");
//  } else {
//   echo(uptime("Up for %days.");
//  }
//

function uptime_init () {
 global $_uptime;
 $fp = fopen("/proc/uptime","r");
 if(!$fp) {
  echo("Unable to open /proc/uptime");
  return FALSE;
 }
 $text = fgets($fp,100);
 fclose($fp);
 $uptime = substr($text,0,strpos($text," "));
 $_uptime["days"] = floor($uptime / 86400);
 $_uptime["hours"] = floor(($uptime - ($_uptime["days"] * 86400)) / 3600);
 $_uptime["mins"] = floor(($uptime - ($_uptime["days"] * 86400) - ($_uptime["hours"] * 3600)) / 60);
 $_uptime["secs"] = floor($uptime - ($_uptime["mins"] * 60) - ($_uptime["days"] * 86400) - ($_uptime["hours"] * 3600));
 return TRUE;
}

function uptime ($string) {
 global $_uptime;
 if(!empty($GLOBALS["uptime"])) $string = uptime;
 if(!is_array($_uptime)) {
  if(!uptime_init()) { return FALSE; }
 }
 return(
  str_replace("%days",$_uptime["days"],
   str_replace("%hours",$_uptime["hours"],
    str_replace("%mins",$_uptime["mins"],
     str_replace("%secs",$_uptime["secs"],$string)
    )
   )
  )
 );
}
?>