i had a few people ask me how can they get the uptime on windows to display it on a webpage like i do on my site on linux.

after doing some research i found its almost impossible unless you:

1) get some exe file from microsoft and execute it to get the uptime.
2) use the operating system pagefile that resets the time everytime the os is rebooted.

so after i found the location of the pagefile i came up with this script:

<?php

$pagefile = 'c:\pagefile.sys'; // Path to system pagefile. Default is c:\\pagefile.sys

function ifif ($value, $true, $false)
{
	if ($value == 0)
	{
		return $false;
	} 
	else
	{
		return $true;
	} 
} 

$upsince = filemtime($pagefile);
$gettime = (time() - filemtime($pagefile));
$days = floor($gettime / (24 * 3600));
$gettime = $gettime - ($days * (24 * 3600));
$hours = floor($gettime / (3600));
$gettime = $gettime - ($hours * (3600));
$minutes = floor($gettime / (60));
$gettime = $gettime - ($minutes * 60);
$seconds = $gettime;

$days   = ifif($days != 1, $days . ' days', $hours . ' day');
$hours   = ifif($hours != 1, $hours . ' hours', $hours . ' hour');
$minutes = ifif($minutes != 1, $minutes . ' minutes', $minutes . ' minute');
$seconds = ifif($seconds != 1, $seconds . ' seconds', $seconds . ' second');

echo 'Server uptime: ' . $days . ' ' . $hours . ' ' . $minutes . ' ' . $seconds;
echo '<br /> Up since: ' . date('l. F jS, Y. h:i a', $upsince);

?>

its only been tested on windows 2000, and windows xp. It works on php 4.x.x, although i don't see why it shouldn't work on php 3.x

if you run a windows webserver, win95,98 i would like to know if it works on it. the system pagefile could possible be located elsewhere on these operating systems.

pagefile.sys is an invisible file, so you would have to search for it using the operating system search tool.

just change c:\ to the letter of your operating system.

    !!That works fine i must say and the first code i ever sean to win GG!!

    That's was just what i looking for and many other people to that i know of tha use Win...

      I found a wee Windows port of the Unix uptime program. Sorry, no link; but there are packages floating around out there with quite a few useful little utilities in them (like uptime, whois, gzip, make, uniq, grep and suchlike).

      <?php
      echo `c:\\unix\\uptime`;
      ?>

        Thank goodness for phpsysinfo

        (I know thats not much use if you're on windows, but... meh!)

          Originally posted by seby


          2) use the operating system pagefile that resets the time everytime the os is rebooted.

          Neat 🙂

            9 days later

            Hi you forgott this...

            
            $days   = ifif($days != 1, $days . ' days', $hours . ' day'); 
            
            shoulde be this or the times get wrong i think!
            
            $days   = ifif($days != 1, $days . ' days', $days . ' day'); 
            
            

              the function ifif() is just a reimplementation of the ?: operator...

              $hours   = ($hours != 1) ? $hours . ' hours' : $hours . ' hour';
              

              or even

              $hours   = $hours.(($hours != 1) ?  ' hours' :  ' hour');
                9 days later

                Originally posted by jayant
                Neat 🙂

                That wouldn't work on my system, because I have disabled the pagefile.

                I don't need it. Windows (98) is stable enough when it's only got 256 mb RAM to play with, and no virtual RAM.

                KITTfan2K

                  6 days later

                  you obviously dont do much with a computer........

                    I have 512MB RAM on my Win98 and I still use about 1gigs of virtual memory for those 3D apps (3DS MAX, Bryce, ...) etc.

                      7 days later

                      diabling the pagefile is not a very good idea.
                      hell, i have 2gb of ram and my virtual memory is 3.5Gb.

                        disabling the page file makes sense from a securiy point of view.

                        Example: The user enters data. You encrypt this data. If that data is only stored in ram the user can easily overwrite the nonencrypted data with the encrypted data and remove all traces of the data from ram this way. However if you're using virtual memory then the data could have been written to the disk and the programmer has no control over when that gets changed.

                          2 years later

                          Hi am I new here but thanks I found this very very helpfull I found it searching google. I have to say I run a server from my house on windows and wanted something like phpsysinfo for my server. And this didn't work at first lol. Because I made a partion only for my paging file on my 200 gig storage drive and had none on c thought it would speed it up since it wasn't loading from c when all the other things were seems it did. But when I first ran the script it say my pc had been up for like 123 days plus or something since like 1942 lol. I don't know how it got that but I added paging file to c and it worked thanks. If my paging file is on say K can I change it to that?

                            Oh one more thing can someone add cpu usage thing to it I want know my cpu usage via web? I have no idea how I am learning php though thanks.

                              10 months later

                              Fix some errors
                              //----- Uptime ------
                              $pagefile = 'c:\pagefile.sys'; // Path to system pagefile. Default is c:\pagefile.sys

                              function ifif ($value, $true, $false)
                              {
                              if ($value < 2)
                              {
                              return $false;
                              }
                              else
                              {
                              return $true;
                              }
                              }

                              $upsince = filemtime($pagefile);
                              $gettime = (time() - filemtime($pagefile));
                              $days = floor($gettime / (24 3600));
                              $gettime = $gettime - ($days
                              (24 3600));
                              $hours = floor($gettime / (3600));
                              $gettime = $gettime - ($hours
                              (3600));
                              $minutes = floor($gettime / (60));
                              $gettime = $gettime - ($minutes * 60);
                              $seconds = $gettime;

                              $days = ifif($days , $days . ' days', $days . ' day');

                              $hours = ifif($hours , $hours . ' hours', $hours . ' hour');
                              $minutes = ifif($minutes , $minutes . ' minutes', $minutes . ' minute');
                              $seconds = ifif($seconds , $seconds . ' seconds', $seconds . ' second');

                                Write a Reply...