PHP 如何获取 Linux 服务器的 uptime

下面两种 PHP 方法都可以获取 Linux 服务器的 uptime:


<?php
$uptime = trim(shell_exec('uptime'));
// output is 04:47:32 up 187 days,  5:03,  1 user,  load average: 0.55, 0.55, 0.54

$uptime = explode(',', $uptime);
$uptime = explode(' ', $uptime[0]);

$uptime = $uptime[2].' '.$uptime[3]; // 187 days
?>

<?php
$uptime = trim(file_get_contents('/proc/uptime'));
$uptime = explode(' ', $uptime);

echo $uptime[0]; //uptime in seconds
?>

热门文章