get system boot time in milliseconds in php

You can use microtime(true) to get the current Unix timestamp with microseconds, and subtract it from the boot time which you can get from the /proc/uptime file.

Here's an example code snippet:

main.php
function getSystemBootTimeInMillis() {
  $uptime = file_get_contents('/proc/uptime');
  $uptimeSeconds = explode(' ', $uptime)[0];
  $bootTimestamp = time() - floatval($uptimeSeconds);
  return round(microtime(true) * 1000) - round($bootTimestamp * 1000);
}

echo getSystemBootTimeInMillis();
293 chars
9 lines

This will output the system boot time in milliseconds. Note that this will only work on Linux systems.

gistlibby LogSnag