[FreeBSD 5.2.1] Uptime in seconds

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
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 :)
 
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.
 
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
 
: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?
 
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.
 
*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 profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back