使用PHP在Linux机器上的Windows转换时间戳更新时间、机器上、PHP、Linux

2023-09-08 12:50:03 作者:寻一世傲骨

我在Linux中,它验证对Active Directory在Windows中运行的企业内部网,通过PHP使用LDAP。

I have an intranet running on a linux box, which authenticates against Active Directory on a Windows box, using LDAP through PHP.

我可以使用LDAP检索用户的公元条目,然后从PHP数组,例如访问的最后登录日期:

I can retrieve a user's entry from AD using LDAP and access the last login date from the php array eg:

echo $adAccount['lastlogontimestamp'][0]; // returns something like 129802528752492619

如果这是一个Unix时间戳我会用下面的PHP code转换为人类可读的日期:

If this was a Unix timestamp I would use the following PHP code to convert to a human readable date:

date("d-m-Y H:i:s", $lastlogontimestamp);

不过,这是行不通的。有谁知道我如何能做到这一点,或事实上,如果有可能从Linux中做到这一点?

However, this does not work. Does anyone know how I can achieve this or indeed if it is possible to do so from a Linux box?

推荐答案

根据这个,窗户时间戳你有100纳秒自1月1日1601。因此,你可能只是它使用下列公式转换为Unix时间戳数量:

According to this, the windows timestamp you have there is the number of 100-ns since Jan 1st 1601. Therefore, you could just convert it to a unix timestamp using the following formula:

tUnix = tWindow/(10*1000*1000)-11644473600;

您除以 10 * 1000 * 1000 来转换为秒自1601年1月1日,然后你打折 11644473600 这是1601年1月和1970年一月(UNIX时间)。

You divide by 10*1000*1000 to convert to seconds since Jan 1st 1601 and then you discount 11644473600 which is the number of seconds between Jan 1601 and Jan 1970 (unix time).

因此​​,在PHP:

date("d-m-Y H:i:s", $lastlogontimestamp/10000000-11644473600);

编辑:有趣的是,我得到了一个不同于巴巴抵消。我有我与Java:

Interestingly, I got a different offset than Baba. I got mine with Java:

Calendar date1 = Calendar.getInstance(); date1.set(1601, 1, 1);
Calendar date2 = Calendar.getInstance(); date2.set(1970, 1, 1);
long dt = date2.getTimeInMillis() - date1.getTimeInMillis();
System.out.println(String.format("%f", dt / 1000.0)); // prints "11644473600.000000"

根据这一SO:方式转换的Unix / Linux的时候Windows时我偏移是正确的。