php 如何将 LDAP 时间戳转换为 Unix 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4647169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert LDAP timestamp to Unix timestamp
提问by Nick
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".
当我使用 PHP 检索 Active Directory 的 LDAP 属性“pwdLastSet”时,我得到类似 1.29265206716E+17 的值。我知道这个值代表日期“2010 年 8 月 17 日星期二 14:11:11 GMT+0200”。
How can I convert this value to a Unix timestamp in PHP? Thanks for any hints!
如何在 PHP 中将此值转换为 Unix 时间戳?感谢您的任何提示!
回答by Stefan Gehrig
Please see here.
请看这里。
Actually it boils down to converting the FILETIME
timestamp into a UNIX timestamp:
实际上它归结为将FILETIME
时间戳转换为 UNIX 时间戳:
$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);
回答by Dennis Kreminsky
回答by jatin
$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.phpfor details
有关详细信息,请参阅http://php.net/manual/en/ref.ldap.php
回答by neobie
@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
@etranger - 更正:应该是 1601 而不是 1600 的时间戳。请参阅微软官方网站:http: //msdn.microsoft.com/en-us/library/ms675243%28v=vs.85%29.aspx
回答by Samuel Harmer
Rather than ask the same question for another language.
而不是对另一种语言提出相同的问题。
Python 3:
蟒蛇3:
from datetime import datetime, timedelta
def ldap2datetime(ts: float):
return datetime(1601, 1, 1) + timedelta(seconds=ts/10000000)
print(ldap2datetime(132255350424395239).isoformat())
# 2020-02-07T07:44:02.439524