get system boot time in php

To get the system boot time in PHP, you can use the exec() function to execute a shell command that retrieves the boot time from the operating system.

Here's the code:

main.php
function getSystemBootTime() {
    $output = shell_exec('uptime -s');
    // 'uptime -s' command returns the boot time in YYYY-MM-DD HH:MM:SS format
    return strtotime(trim($output));
}

echo 'System boot time: ' . date('Y-m-d H:i:s', getSystemBootTime());
259 chars
8 lines

The shell_exec() function runs the uptime -s command, which returns the system boot time in the format YYYY-MM-DD HH:MM:SS. The trim() function is used to remove any extra white space in the output, and the strtotime() function is used to convert the boot time string to a Unix timestamp.

Finally, we print the system boot time in the desired format using the date() function.

related categories

gistlibby LogSnag