1

我需要从 PHP 脚本中获取上次系统启动时间,最好是 UNIX 时间戳格式。

是否有在引导时创建或修改的文件?我可以读取文件的修改时间。

系统类型为 CentOS。

4

3 回答 3

7

/proc/正常运行时间

该文件包含两个数字:系统的正常运行时间(秒)和空闲进程所花费的时间(秒)。

<?php

function get_boottime() {
    $tmp = explode(' ', file_get_contents('/proc/uptime'));

    return time() - intval($tmp[0]);
}

echo get_boottime() . "\n";
于 2012-09-12T11:48:18.063 回答
2

尝试这个:

方法一

<?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[0] );

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

希望能帮助到你。

于 2012-09-12T11:53:06.517 回答
2
who -b # in command-line

Gives you the last boot time (http://www.kernelhardware.org/find-last-reboot-time-and-date-on-linux/)

exec ( $command , $output ); // in PHP

Executes a shell command

strtotime(); // in PHP

Parse about any English textual datetime description into a Unix timestamp

So this will give you the Linux System last boot time :

// in PHP
exec('who -b',$bootTime);//where $bootTime is an array, each line returned by the command is an element of the array
$result = strtotime($bootTime[1]);
于 2012-09-12T11:56:20.927 回答