从PHP脚本中,我需要获得最后一次系统启动时间,最好是UNIX时间戳格式。
是否有在启动时创建或修改的文件?我可以读取文件的修改时间。
系统类型为CentOS。
/proc/uptime
该文件包含两个数字:系统的正常运行时间(秒)和在空闲进程中花费的时间(秒钟)。
<?php
function get_boottime() {
$tmp = explode(' ', file_get_contents('/proc/uptime'));
return time() - intval($tmp[0]);
}
echo get_boottime() . "'n";
试试这个:
方法1
<?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
?>
方法2
<?php
$uptime = trim( file_get_contents( '/proc/uptime' ) );
$uptime = explode( ' ', $uptime[0] );
echo $uptime[0];//uptime in seconds
?>
希望能有所帮助。
who -b # in command-line
给你最后一次开机时间(http://www.kernelhardware.org/find-last-reboot-time-and-date-on-linux/)
exec ( $command , $output ); // in PHP
执行外壳命令
strtotime(); // in PHP
将任何英文文本日期时间描述解析为Unix时间戳
因此,这将为您提供Linux系统的最后一次启动时间:
// 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]);