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/)仅为分享信息绝非推荐,网站不参与交易绝非中介,内容均仅代表个人观点绝非权威,所有信息仅供参考,读者请自行考虑后入手并自担风险!一分钱一分货仍是恒久不变之真理,未成年读者(包括生理和心理)请在监护人陪同下访问本站!
除非注明,沙唐桔文章均为原创,本文地址 https://cyhour.com/844/AMP 移动加速版本)转载时必须以链接形式注明原始出处。
Vultr 送$100,搬瓦工年付最低$49,优惠码 BWH3HYATVBJW,更多推荐VPS信息,或支持老杨
Views: 1.5K Tags:  , 

Comments:0

发表留言

Vultr 送$100,搬瓦工年付最低$49,优惠码 BWH3HYATVBJW,更多推荐VPS信息