How can PHP be used to retrieve the last system startup time using WMI?
To retrieve the last system startup time using WMI in PHP, we can utilize the `COM` class to interact with WMI objects. We need to query the `Win32_OperatingSystem` class and retrieve the `LastBootUpTime` property to get the system startup time. By parsing the returned date/time string, we can convert it into a more readable format.
<?php
$wmi = new COM('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2');
$os = $wmi->ExecQuery('SELECT LastBootUpTime FROM Win32_OperatingSystem');
foreach ($os as $obj) {
$lastBootUpTime = $obj->LastBootUpTime;
$lastBootUpTime = preg_replace('/(\.\d+)?\+(\d+)$/', '', $lastBootUpTime);
$lastBootUpTime = date('Y-m-d H:i:s', strtotime($lastBootUpTime));
echo "Last system startup time: $lastBootUpTime";
}
?>
Related Questions
- What potential pitfalls should I be aware of when setting and accessing cookies in PHP?
- What resources or documentation should be consulted to better understand and troubleshoot PHP forum customization issues related to user recognition and permissions?
- What are some best practices for securely handling form data in PHP scripts?