How to convert LDAP timestamp to Unix timestamp

拟墨画扇 提交于 2019-12-28 13:21:52

问题


When I retrieve the LDAP attribute "pwdLastSet" of an Active Directory using PHP I get a value like 1.29265206716E+17. I know that this value represents the date "Tue Aug 17 2010 14:11:11 GMT+0200".

How can I convert this value to a Unix timestamp in PHP? Thanks for any hints!


回答1:


Please see here.

Actually it boils down to converting the FILETIME timestamp into a UNIX timestamp:

$fileTime = "130435290000000000";
$winSecs       = (int)($fileTime / 10000000); // divide by 10 000 000 to get seconds
$unixTimestamp = ($winSecs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
echo date(DateTime::RFC822, $unixTimestamp);



回答2:


$dateLargeInt= "1.29265206716E+17"; // nano seconds since jan 1st 1601
$secsAfterADEpoch = $dateLargeInt / (10000000); // seconds since jan 1st 1601
$ADToUnixConvertor=((1970-1601) * 365.242190) * 86400; // unix epoch - AD epoch * number of tropical days * seconds in a day
$unixTsLastLogon=intval($secsAfterADEpoch-$ADToUnixConvertor); // unix Timestamp version of AD timestamp
$lastlogon=date("d-m-Y", $unixTsLastLogon); // formatted date

See http://php.net/manual/en/ref.ldap.php for details




回答3:


@etranger - correction: should be timestamp from 1601 instead of 1600. Refer to offical microsoft website: http://msdn.microsoft.com/en-us/library/ms675243%28v=vs.85%29.aspx




回答4:


There's this page suggesting that it is "100-nanosecond units passed since 1.1.1601 00:00:00", this might be helpful.

EDIT: 1600 »» 1601




回答5:


I think the first step is to understand what the LDAP values really mean - once you get that the rest is easy.

There are few LDAP attributes that deal with dates. pwdLastSet and accountExpires have values like 127524839567199000 and whenChanged has values like 20050210223453.0Z - and both values refer to the same date 10-FEB-2005.

For a simple explanation please see http://maxvit.net/convert_ldap_dates

Hope this helps!



来源:https://stackoverflow.com/questions/4647169/how-to-convert-ldap-timestamp-to-unix-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!