Unix 时间(以毫秒为单位)换算为 dmY

发布于 2025-01-04 08:40:33 字数 1301 浏览 0 评论 0原文

我正在从 xml 文档中提取以毫秒为单位的 unix 时间,但无法将其转换为 php 5.2.17 中的可读日期 (dmY)。

我使用了 http://www.epochconverter.com/ 可以正确转换它 从13287239267762/9/2012 13:37:49

我尝试了以下操作:

$timestamp = $xml['LastBackupDate'];
echo '<br />Method 1: '.date("d-m-Y", $timestamp);
echo '<br />Method 2: '.date("d-m-Y", strtotime($timestamp));
echo '<br />Method 3: '.date("d-m-Y", strtotime($timestamp*1000));
echo '<br />Method 4: '.date("d-m-Y", strtotime($timestamp/1000));
echo '<br />Method 5: '.date("d-m-Y", $timestamp/1000);
echo '<br />Method 6: '.date("d-m-Y", (int)$timestamp);
echo '<br />Method 7: '.date("d-m-Y", intval($timestamp));
echo '<br />Method 8: '.date("d-m-Y", intval($timestamp)/1000);
echo '<br />Method 9: '.date("d-m-Y", intval($timestamp/1000));

这会返回:

Method 1: 
Method 2: 01-01-1970
Method 3: 01-01-1970
Method 4: 01-01-1970
Method 5: 25-01-1970
Method 6: 19-01-2038
Method 7: 19-01-2038
Method 8: 25-01-1970
Method 9: 25-01-1970

有什么想法如何转换它吗? 谢谢!


编辑,使用的最佳结果:

echo date("d-m-Y", substr($xml['LastBackupDate'],0,-3));

I'm pulling a unix time in milliseconds from an xml document but can't convert it to a readable date (d-m-Y) in php 5.2.17.

I've used http://www.epochconverter.com/ which converts it correctly
from 1328723926776 to 2/9/2012 13:37:49

I've tried the following:

$timestamp = $xml['LastBackupDate'];
echo '<br />Method 1: '.date("d-m-Y", $timestamp);
echo '<br />Method 2: '.date("d-m-Y", strtotime($timestamp));
echo '<br />Method 3: '.date("d-m-Y", strtotime($timestamp*1000));
echo '<br />Method 4: '.date("d-m-Y", strtotime($timestamp/1000));
echo '<br />Method 5: '.date("d-m-Y", $timestamp/1000);
echo '<br />Method 6: '.date("d-m-Y", (int)$timestamp);
echo '<br />Method 7: '.date("d-m-Y", intval($timestamp));
echo '<br />Method 8: '.date("d-m-Y", intval($timestamp)/1000);
echo '<br />Method 9: '.date("d-m-Y", intval($timestamp/1000));

which brings back:

Method 1: 
Method 2: 01-01-1970
Method 3: 01-01-1970
Method 4: 01-01-1970
Method 5: 25-01-1970
Method 6: 19-01-2038
Method 7: 19-01-2038
Method 8: 25-01-1970
Method 9: 25-01-1970

Any ideas how to convert this?
Thanks!


Edit, Best result from using:

echo date("d-m-Y", substr($xml['LastBackupDate'],0,-3));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-01-11 08:40:33

您是否尝试过除以1000而不是乘法?纪元时间通常以秒为单位...


好吧,现在我实际上需要学习 PHP ...

date 需要一种格式和时间。
strtotime 接受人类可读的日期/时间字符串并返回时间。您的值是以毫秒为单位的纪元时间,而不是人类可读的字符串,因此不是 strtotime 的有效参数。

尝试不带 strtotime方法 4,不需要它。


是的,这不起作用,因为 $timestamp 是一个字符串(我假设它已经是一个整数)。

你说删除字符串的最后 3 个字符可以得到大部分的结果,这与转换为整数然后除以 1,000 相同。你的方法6& 7 转换为 int 但不除 - 你两者都需要!

根据 thisdate 需要 < em>整数时间戳以秒为单位。您拥有的值是一个字符串,因此您应该将其转换为整数。它也以毫秒为单位,因此您必须除以 1,000 才能得到正确的单位。

请注意,纪元时间自 1970 年 1 月 1 日起按秒计算。因此,任何时候您看到 01-01-1970,第二个参数都会计算为零。

date("d-m-Y", $timestamp);                 <-- string, not int: treated as zero
date("d-m-Y", strtotime($timestamp));      <-- not a formatted time, gives zero
date("d-m-Y", strtotime($timestamp*1000)); <-- ditto
date("d-m-Y", strtotime($timestamp/1000)); <-- ditto
date("d-m-Y", $timestamp/1000);            <-- clearly nonzero, but no idea what!
date("d-m-Y", (int)$timestamp);            <-- correct type but in milliseconds
date("d-m-Y", intval($timestamp));         <-- correct type but in milliseconds

如果您传递一个包含秒数而不是毫秒数的整数,那么它应该是正确的。

就像这样:

date("d-m-Y", intval($timestamp)/1000);

Have you tried dividing by 1000, instead of multiplying? Epoch time is usually in seconds ...


OK, so now I actually need to learn PHP ...

date takes a format and a time.
strtotime takes a human-readable date/time string and returns a time. Your value is epoch time in milliseconds, not a human-readable string, so isn't a valid argument to strtotime.

Try Method 4 without the strtotime, it shouldn't be needed.


Right, that didn't work, because $timestamp is a string (I'd assumed it was already an integer).

You say that dropping the last 3 characters of the string gets you most of the way, that is the same as converting to an integer and then dividing by 1,000. Your methods 6 & 7 convert to int but don't divide - you need both!

According to this, date requires an integer timestamp in seconds. The value you have is a string, so you should convert it to an integer. It is also in milliseconds, so you have to divide by 1,000 to get the right units.

Note that epoch time is counted as seconds since the first of January, 1970. So, any time you see 01-01-1970, the second argument evaluated to zero.

date("d-m-Y", $timestamp);                 <-- string, not int: treated as zero
date("d-m-Y", strtotime($timestamp));      <-- not a formatted time, gives zero
date("d-m-Y", strtotime($timestamp*1000)); <-- ditto
date("d-m-Y", strtotime($timestamp/1000)); <-- ditto
date("d-m-Y", $timestamp/1000);            <-- clearly nonzero, but no idea what!
date("d-m-Y", (int)$timestamp);            <-- correct type but in milliseconds
date("d-m-Y", intval($timestamp));         <-- correct type but in milliseconds

If you pass an integer containing a number of seconds rather than milliseconds, it should be correct.

Like so:

date("d-m-Y", intval($timestamp)/1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文