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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:51:00  来源:igfitidea点击:

How to convert LDAP timestamp to Unix timestamp

phpldaptimestamp

提问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 FILETIMEtimestamp 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

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

这个页面表明它是“自 1.1.1601 00:00:00 以来传递的 100 纳秒单位”,这可能会有所帮助。

EDIT: 1600 ?? 1601

编辑:1600 ?? 1601

回答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