[php] Uptime in FreeBSD

Discussion in 'Web Design & Coding' started by sean.ferguson, Jul 17, 2004.

  1. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    im using freebsd4.10-RELEASE running Apache 2 and PHP5.
    I want something so i can see the server's uptime in a php file. (eg ~/uptime.php)
     
  2. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    got it:

    Code:
    <?
    function uptime() {
    	$s = explode( " ", exec("/sbin/sysctl -n kern.boottime") );
    	$a = str_replace( ",", "", $s[3]);
    	$sys_ticks = time() - $a;
    
    	$min   = $sys_ticks / 60;
    	$hours = $min / 60;
    	$days  = floor( $hours / 24 );
    	$hours = floor( $hours - ($days * 24) );
    	$min   = floor( $min - ($days * 60 * 24) - ($hours * 60) );
    
    	if( $days ) {
    		$result = "$days day";
    		if( $days != 1 ) $result .= "s";
    		$result .= " ";
    	}
    
    	if( $hours ) {
    		$result .= "$hours hour";
    		if( $hours != 1 ) $result .= "s";
    		$result .= " ";
    	}
    
    	$result .= "$min minute";
    	if( $min != 1 ) $result .= "s";
    
    	return $result;
    }
    
    echo uptime();
    ?>
    
     
  3. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    On my Windows server I used this:
    Code:
    $uptime = exec("uptime.exe");
    //breaking at 'for:'
    $uptime = explode("for:", $uptime);
    //only using the last bit, since I am not interested in the stuff before 'for:'
    $uptime = $uptime[1];
    The uptime.exe utility used a format like
    Code:
    \\<computername> has been running for: d day(s), h hour(s) and m minute(s)
    The easiest way would be using an uptime util that uses the Unix timestamp format, so you can format the time and date yourself. I am still looking for one for myself, so keep me posted :) (also for freebsd 4.10)
     
  4. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Argh! I typed too long again :p Thanks for sharing that with us :)
     
  5. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    WOrks like a charm :) Thanks :D
     
  6. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    no problem, i got it thanks to a friend on irc :)