如何将 LDAP "whenCreated" 属性时间戳 20150527135501.0Z 转换为 PHP 中的时间戳?

How to convert LDAP "whenCreated" attribute timestamp 20150527135501.0Z to timestamp in PHP?

如何将 LDAP "whenCreated" 属性时间戳 20150527135501.0Z 转换为 PHP 中的时间戳。

$justthese = array("displayname","department" , "title","samaccountname", "mail", "whenCreated"); 

$result = ldap_search($ldap,$dn , $filter ,$justthese) or die ("Search failed");
 $info = ldap_get_entries($ldap, $result);
print_r ($info);
for ($i=0; $i < $info["count"]; $i++) { 
echo "Name: ".$info[$i]["displayname"][0]."<br>\n"; 
echo "Department: ".$info[$i]["department"][0]."<br>\n"; 
echo "Created: ".$info[$i]["whenCreated"][0]."<br>\n"; 
}

我得到了用户的显示名称和 department.But 我没有得到 whencreated 时间戳。在我的 print_r ($info);

我得到 20150527135501.0Z 作为 timestamp.How 我可以在 PHP 中打印这个时间戳吗?

问题可能与属性的大小写有关,因为它是从 LDAP 返回的。而是尝试:

echo "Created: ".$info[$i]["whencreated"][0]."<br>\n";

如果还是不行,我需要从 var_dump.

查看 $info 数组的结构

要获得一个实际的 \DateTime 对象,然后您可以按照您想要的任何方式对其进行格式化,您可以这样做:

    preg_match("/^(\d+).?0?(([+-]\d\d)(\d\d)|Z)$/i", $info[$i]["whencreated"][0], $matches);
    if (!isset($matches[1]) || !isset($matches[2])) {
        throw new \Exception(sprintf('Invalid timestamp encountered: %s', $info[$i]["whencreated"][0]));
    }
    $tz = (strtoupper($matches[2]) == 'Z') ? 'UTC' : $matches[3].':'.$matches[4];
    $date = new \DateTime($matches[1], new \DateTimeZone($tz));

    // Now print it in any format you like
    echo $date->format('Y-m-d H:i:s');

最好把上面的变成一个函数。