还是初学者所以请耐心等待......
所以我发现这个函数用于系统正常运行时间并且一直在讨论它,因为我一般都学习php和web开发。
我的目标是让输出看起来像天:小时:分钟:秒但是没有$ seconds变量所以我根据我的其他内容添加了该行。
一切都很好,除了秒只显示为0.我不太确定我做错了什么,或者这是否是最好的方法。
function Uptime() {
$uptime = @file_get_contents( "/proc/uptime");
$uptime = explode(" ",$uptime);
$uptime = $uptime[0];
$days = explode(".",(($uptime % 31556926) / 86400));
$hours = explode(".",((($uptime % 31556926) % 86400) / 3600));
$minutes = explode(".",(((($uptime % 31556926) % 86400) % 3600) / 60));
$seconds = explode(".",((((($uptime % 31556926) % 86400) % 3600) / 60) / 60));
$time = $days[0].":".$hours[0].":".$minutes[0].":".$seconds[0];
return $time;
}
编辑:我能够以不同的方式使用新功能。 如果有人能回答为什么上述方法没有按预期工作,并且下面的新方法是实现这一目标的最佳方法,我仍然很好奇。
function Uptime() {
$ut = strtok( exec( "cat /proc/uptime" ), "." );
$days = sprintf( "%2d", ($ut/(3600*24)) );
$hours = sprintf( "%2d", ( ($ut % (3600*24)) / 3600) );
$min = sprintf( "%2d", ($ut % (3600*24) % 3600)/60 );
$sec = sprintf( "%2d", ($ut % (3600*24) % 3600)%60 );
return array( $days, $hours, $min, $sec );
}
$ut = Uptime();
echo "Uptime: $ut[0]:$ut[1]:$ut[2]:$ut[3]";
编辑2:我认为最后一种方法是最好的,基于nwellnhof给出的答案。我不得不调整一下以获得完全符合我想要的输出。谢谢你们。
function Uptime() {
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$secs = $num % 60;
$num = (int)($num / 60);
$mins = $num % 60;
$num = (int)($num / 60);
$hours = $num % 24;
$num = (int)($num / 24);
$days = $num;
return array(
"days" => $days,
"hours" => $hours,
"mins" => $mins,
"secs" => $secs
);
}
答案 0 :(得分:6)
直接从/proc/uptime
阅读是Linux上最有效的解决方案。有多种方法可将输出转换为天/小时/分钟/秒。尝试类似:
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$secs = fmod($num, 60); $num = (int)($num / 60);
$mins = $num % 60; $num = (int)($num / 60);
$hours = $num % 24; $num = (int)($num / 24);
$days = $num;
或者,intdiv
(PHP7):
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$secs = fmod($num, 60); $num = intdiv($num, 60);
$mins = $num % 60; $num = intdiv($num, 60);
$hours = $num % 24; $num = intdiv($num, 24);
$days = $num;
答案 1 :(得分:0)
ConnectionHandler
支持uptime
命令行选项。你可以使用这段简单的代码:
-p
答案 2 :(得分:0)
如果只是查看语句中的模式,可以看到秒中的模式不同。它有两个部门。此外,您使用的数字表示每个时间单位的秒数。每秒的秒数应为1,而不是60.简而言之:
$seconds = explode(".",((((($uptime % 31556926) % 86400) % 3600) / 60) / 60));
应该是:
$seconds = explode(".",((((($uptime % 31556926) % 86400) % 3600) % 60) / 1));
现在整个做事方式有点奇怪。例如,(x % (n*m)) % m
只是x % m
。
更好的方法是:
$uptime = (int) $uptime;
$seconds = $uptime % 60;
$minutes = ($uptime / 60 ) % 60;
$hours = ($uptime / (60*60) ) % 24;
$days = $uptime / (60*60*24); # % 365, if you want
答案 3 :(得分:0)
您作为班级的初始示例的变体:
class Uptime {
private $uptime;
private $modVals = array(31556926, 86400, 3600, 60, 60);
public function __construct() {
$this->read_uptime();
}
/**
* actually trigger a read of the system clock and cache the value
* @return string
*/
private function read_uptime() {
$uptime_raw = @file_get_contents("/proc/uptime");
$this->uptime = floatval($uptime_raw);
return $this->uptime;
}
private function get_uptime_cached() {
if(is_null($this->uptime)) $this->read_uptime(); // only read if not yet stored or empty
return $this->uptime;
}
/**
* recursively run mods on time value up to given depth
* @param int $d
* @return int
**/
private function doModDep($d) {
$start = $this->get_uptime_cached();
for($i=0;$i<$d;$i++) {
$start = $start % $this->modVals[$i];
}
return intval($start / $this->modVals[$d]);
}
public function getDays()
{
return $this->doModDep(1);
}
public function getHours() {
return $this->doModDep(2);
}
public function getMinutes()
{
return $this->doModDep(3);
}
public function getSeconds()
{
return $this->doModDep(4);
}
public function getTime($cached=false) {
if($cached != false) $this->read_uptime(); // resample cached system clock value
return sprintf("%03d:%02d:%02d:%02d", $this->getDays(), $this->getHours(), $this->getMinutes(), $this->getSeconds());
}
}
答案 4 :(得分:0)
在Unix / BSD上,使用/proc
是不可靠的,因为默认情况下没有安装它,在某些Linux发行版上它也可以卸载,所以使用{{1}解析它会更好}或uptime
命令,例如
sysctl
sysctl
或:
<?php
preg_match('/sec = (\d+)/', shell_exec('sysctl -n kern.boottime'), $secs)
echo $secs[1];
或根据m0n0wall提供的示例:
$s = explode( " ", exec("/sbin/sysctl -n kern.boottime") );
$a = str_replace( ",", "", $s[3]);
$uptime = time() - $a;
<?php
exec("/sbin/sysctl -n kern.boottime", $boottime);
preg_match("/sec = (\d+)/", $boottime[0], $matches);
$boottime = $matches[1];
$uptime = time() - $boottime;
if ($uptime > 60)
$uptime += 30;
$updays = (int)($uptime / 86400);
$uptime %= 86400;
$uphours = (int)($uptime / 3600);
$uptime %= 3600;
$upmins = (int)($uptime / 60);
$uptimestr = "";
if ($updays > 1)
$uptimestr .= "$updays days, ";
else if ($updays > 0)
$uptimestr .= "1 day, ";
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
echo htmlspecialchars($uptimestr);
取自4webhelp:
的示例uptime
或(在FreeBSD上测试):
<?php
$data = shell_exec('uptime');
$uptime = explode(' up ', $data);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];
echo ('Current server uptime: '.$uptime.'
这是适用于Windows的版本:
$uptime = exec("uptime");
$uptime = split(" ",$uptime);
$days = $uptime[3]; # NetBSD: $days = $uptime[4];
$time = split(",",$uptime[5]); # NetBSD: $time = split(",",$uptime[7]);
if (sizeof($hourmin = split(":",$time[0])) < 2){ ;
$hours = "0";
$mins = $hourmin[0];
} else {
$hourmin=split(":",$time[0]);
$hours = $hourmin[0];
$mins = $hourmin[1];
}
$calcuptime = "Uptime: ".$days." days ".$hours." hours ".$mins." mins" ;
echo $calcuptime;