get system boot time in seconds in php

Here's an example code to get the system boot time in seconds using shell_exec() in php:

main.php
$output = shell_exec("uptime -s"); // get the last system start time
$start_time = strtotime(trim($output)); // convert the string to a timestamp
$current_time = time(); // get current time as timestamp
$boot_time = $current_time - $start_time; // calculate the time since last system start

echo "System boot time: $boot_time seconds";
337 chars
7 lines

In this code, we use the shell_exec() function to execute a shell command (uptime -s) that returns the last system start time as a string. We then convert this string to a timestamp using the strtotime() function. Finally, we calculate the time difference between the current time and the system boot time, and print it to the console.

gistlibby LogSnag