With this snippet you can print the uptime of a MySQL server from a PHP script. It uses PDO to make the connection.
If you know a simpler way to do this, feel free to post a comment!
try
{
$dbh = new PDO('mysql:host=localhost;dbname=database_name', 'user', 'password');
foreach($dbh->query("SHOW GLOBAL STATUS LIKE 'Uptime'") as $row)
{
echo ' MySQL server OK, uptime ';
echo ($row['Value'] >= 31557000) ? ' >365 days' : (gmdate("z", ($row['Value'])) . ' days, ' . gmdate("H:i", $row['Value']));
}
}
catch (PDOException $e)
{
print "MySQL server error: <br/> " . $e->getMessage();
}
The result will look something like this:
15 days, 04:38