• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[FreeBSD 5.2.1] Uptime in seconds

#1
Is it possible to display the uptime in FreeBSD in seconds only? Now I get a whole bunch of information, so it's very difficult to format it in a PHP script, like you see on my new homepage: http://glaanieboy.dyndns.org/develop (bottom right). When I get the uptime in seconds, I can format the time/date format using date(). Thanks :)
 

X-Istence

*
Political User
#2
sedated# sysctl -a | grep kern.boottime
kern.boottime: { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004

Parse it with PHP, and you are set. That was the exact second that the server was booted. take a look at phpSysInfo for more clues on how to do this.
 

j79zlr

Glaanies script monkey
Political User
#3
I was bored, here ya go,

PHP:
<?php 
  // Get boottime in seconds
  $boottime = shell_exec('sysctl kern.boottime');

  // Explode and return array 
  $a = explode (" ", $boottime);

  // Remove trailing comma
  $b = ereg_replace('\,', '', $a[4]);

  // Find uptime in seconds
  $uptime = time() - $b;

  // Determine days, hours, etc
  $days = floor($uptime/60/60/24);
  $hours = $uptime/60/60%24;
  $mins = $uptime/60%60;
  $secs = $uptime%60;

  // Display time
  echo "Uptime $days days $hours hours $mins minutes and $secs seconds.";
?>
http://j79zlr.homeunix.com/misc/uptime.php
 
#4
:p Thanks! Save me some time :)
But now another question: I restarted the server yesterday at 7:10pm, yet the system boottime is set at 5:10pm? How can that happen? How can I solve that?
 

Reg

eXperienced!
#5
This would be caused by the time on the machine (in the BIOS) not being correct. Check to make sure that the system time is correct.

From the shell, run "uptime." The number on the far left would be the current system time.
 
#7
*bump* Well, I finally integrated j79zlr's code into my site and it works like a charm. About the 2 hours difference, the BIOS is set at the correct time, so I guess the server's time is set at GMT, eventhough I set the time at GMT+1 (+ another 1 for the daylight savings) at install. I solved it by adding $b+=date("Z") in the code, date("Z") is the time difference in seconds from GMT, so that was easily solved :)
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies