在 Linux Box 上使用 PHP 将 Windows 时间戳转换为日期

2024-04-13php开发问题
6

本文介绍了在 Linux Box 上使用 PHP 将 Windows 时间戳转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个在 linux 机器上运行的 Intranet,它通过 PHP 使用 LDAP 对 Windows 机器上的 Active Directory 进行身份验证.

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

我可以使用 LDAP 从 AD 中检索用户的条目,并从 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 代码转换为人类可读的日期:

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?

推荐答案

根据this,您拥有的 windows 时间戳是自 1601 年 1 月 1 日以来 100 ns 的数量.因此,您可以使用以下公式将其转换为 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 年 1 月之间的秒数(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);

有趣的是,我得到的偏移量与 Baba 不同.我用 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 时间的方法我的偏移量是正确的.

According to this SO: Ways to Convert Unix/Linux time to Windows time my offset is correct.

这篇关于在 Linux Box 上使用 PHP 将 Windows 时间戳转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17