An Improved Linux Uptime Function

This is an improvement over this post. This also produces an uptime, but properly splits the seconds in “days”, “hours”, “minutes” & “seconds”, as well as appending s or not if it should be there.

Could further be improved to include options to be called from the function.

function uptime() {
	$procuptime = popen("cat /proc/uptime | awk -F \" \" '{print $1}'", "r");
	$read = fread($procuptime, 2096);
	$day = floor($read / 86400);
	$hour = floor(($read % 86400) / 3600);
	$minute = floor((($read % 86400) % 3600) / 60);
	$second = floor(((($read % 86400) % 3600) % 60));
			
	if ($day == "1") {
		echo "$day day ";
	} else {
		echo "$day days ";
	}
	if ($hour == "1") {
		echo "$hour hour ";
	} else {
		echo "$hour hours ";
	}
	if ($minute == "1") {
		echo "$minute minute ";
	} else {
		echo "$minute minutes ";
	} 
	if ($second == "1") {
		echo "$second second ";
	} else {
		echo "$second seconds ";
	} 
}	

Posted at 1:35 on 1 nd June 2009