Server Statistics - Uptime in Percentage
(Page 2 of 4 )
If you looked at the last code you saw two redundant and useless date() functions. Well in this section they will be used to work out what percentage of the year so far the server has been online. All we do is add a line to calculate the percentage uptime.
Here is the whole block of code to work out the percentage of the year so far.
<?php $start=mktime(0, 0, 0, 1, 1, date("Y"), 0); /* Make date 1/1/(current year) */ $end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0); /* Make todays date */ $diff=$end-$start; $days=$diff/86400; $percentage=($uptime/$days) * 100; /* Work out percentage */ echo $percentage; /* Print out percentage */ ?> |
This could be used for anything, firstly we use $start and mktime to acquire the number of seconds since 1/1/(the current year) and then we aquire today’s date using the same function. To work out how long in seconds the server has been online we take $start from $end. Now to work out the days we divide the result by 86400 and we will get a number of days which then we divide by $uptime (obtained from the first code snippet) and multiply by 100 to get a percentage. Simple wasn’t it?
Next we will work on a more complex version, this will include listing of server variables like the server name, IP, port, software and gateway.
Next: Final Script >>
More Programming Basics Articles
More By Andrew Walsh