LDAP PHP 上次登录时间戳


LDAP PHP last logon timestamp

我有这个代码,可以获取计算机的标签,操作系统和上次登录时间戳。

问题是我无法显示时间戳代码,以便人类可以理解它。

有没有办法在此代码中实现这一点?

    <?php
header('Content-Type: text/html; charset=iso-8859-1');
$host = "ldap://server.server.net";
$user = "domain'user";
$pswd = "passw";
$ad = ldap_connect($host)
      or die( "Could not connect!" );

ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
     or die ("Could not set ldap protocol");
// Binding to ldap server
$bd = ldap_bind($ad, $user, $pswd)
      or die ("Could not bind");
// Create the DN
$dn = "OU=NotebookM2,OU=WorkstationsM2,OU=PT1,DC=heiway,DC=net";

$attrs = array("cn","operatingsystem","lastlogon");
// Create the filter from the search parameters
$filter = $_POST['filter']."=".$_POST['keyword']."*";
$search = ldap_search($ad, $dn, $filter, $attrs)
          or die ("ldap search failed");
$entries = ldap_get_entries($ad, $search);
if ($entries["count"] > 0) {
  echo "<table border='1' width='100%'>";
  echo "<tr>";
    echo "<td>TAG:</td>";
  echo "<td>OS:</td>";
  echo "<td>Last Logon:</td>";
  echo "</tr>";
for ($i=0; $i<$entries["count"]; $i++) {
  echo "<tr>";
   echo "<td>".$entries[$i]["cn"][0]."</td>";
   echo "<td>".$entries[$i]["operatingsystem"][0]."</td>";
   echo "<td>".$entries[$i]["lastlogon"][0]."</td>";
  echo "</tr>";
}
  echo "</table>";
} else {
   echo iconv("UTF-8", "ISO-8859-1","<p>Ops Nothing Found</p>");
}
ldap_unbind($ad);
?>

尝试使用这个添加此函数

function ldapTimeToUnixTime($ldapTime) {
    $unixTime = 0;
    /* LDAP time is assumed to start with "YYYYMMDDhhmmss" and to be in a 
     * Greenwith Meantime format.
     */
    $pat = '/^([0-9]{4,4})([0-9]{2,2})([0-9]{2,2})([0-9]{2,2})([0-9]{2,2})' .
    '([0-9]{2,2}).*/';
    if(preg_match($pat, $ldapTime, $matches) > 0) {
            $unixTime = gmmktime($matches[4], $matches[5], $matches[6],
              $matches[2], $matches[3], $matches[1]);
    }
    return $unixTime;
}

替换行

echo "<td>".$entries[$i]["lastlogon"][0]."</td>";

$timestamp=ldapTimeToUnixTime($entries[$i]["lastlogon"][0]);
echo "<td>". gmdate("Y-m-d'TH:i:s'Z", $timestamp) ."</td>";