PHP 获取服务器运行时间(system uptime)

如题,PHP 获取 VPS 服务器运行时间(system uptime),在 WordPress 模板中使用显示出来。

代码提取自雅黑探针,PHP7 下测试通过。

if ( !function_exists( 'sys_uptime' ) ) {
function sys_uptime() {
$output='';
if (false === ($str = @file("/proc/uptime"))) return false;
$str = explode(" ", implode("", $str));
$str = trim($str[0]);
$min = $str / 60;
$hours = $min / 60;
$days = floor($hours / 24);
$hours = floor($hours - ($days * 24));
$min = floor($min - ($days * 60 * 24) - ($hours * 60));
if ($days !== 0) $output .= $days."天";
if ($hours !== 0) $output .= $hours."小时";
if ($min !== 0) $output .= $min."分钟";
return $output;
}
}

其它代码:How to display system uptime in php?

$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;

#Or, with 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;
声明:除非注明,常阳时光文章均为原创
本文地址:https://cyhour.com/844/
转载时必须以链接形式注明原始出处。
Views: 172 Tags:  , 

Comments:0

发表留言

炒蛋:Vultr 送$25 | 搬瓦工年付$19补货,优惠码 BWH26FXH3HIQ | 推荐VPS信息