使用 PHP 从 XML 提要中提取时间戳,但似乎有很多位数

发布于 2024-08-26 11:50:57 字数 483 浏览 4 评论 0原文

我从 feed 中提取时间戳,它给出 12 位数字 (1269088723811)。当我转换它时,它显示为

1901-12-13 20:45:52,

但是如果我将时间戳放入 http://www.epochconverter.com/ 显示为

2010 年 3 月 20 日星期六 12:38:43 GMT,这是正确的时间。

来处理它

epochconverter.com 提到它可能以毫秒为单位,因此我修改了脚本以使用$mil = $timestamp;
$秒 = $百万/1000;
$date = date('Ymd H:i:s', date($秒));

但它仍然将日期转换为错误的,1970-01-25 20:31:23。

我做错了什么?

I am pulling a timestamp from a feed and it gives 12 digits (1269088723811). When I convert it, it comes out as

1901-12-13 20:45:52,

but if I put the timestamp into http://www.epochconverter.com/ it comes out as

Sat, 20 Mar 2010 12:38:43 GMT, which is the correct time.

epochconverter.com mentions that it maybe in milliseconds so I have amended the script to take care of it using


$mil = $timestamp;
$seconds = $mil / 1000;
$date = date('Y-m-d H:i:s', date($seconds));

but it still converts the date wrong, 1970-01-25 20:31:23.

What am I doing wrong?

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

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

发布评论

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

评论(2

格子衫的從容 2024-09-02 11:50:57

这似乎是经过修剪的 microtime() 输出。您似乎做错的唯一一件事是使用 date($seconds) 而不是原始的 $seconds。尝试

$date = date('Y-m-d H:i:s', $seconds); 

This seems to be a trimmed microtime() output. The only thing you seem to be doing wrong is using date($seconds) instead of the raw $seconds. Try

$date = date('Y-m-d H:i:s', $seconds); 
情绪 2024-09-02 11:50:57
$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
echo date('Y-m-d H:i:s', (float)$xml->a / 1000);

$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
$ts = substr($xml->a, 0, -3);
echo date('Y-m-d H:i:s', $ts);

都打印 2010-03-20 07:38:43 (在我的欧洲/柏林机器上)

$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
echo date('Y-m-d H:i:s', (float)$xml->a / 1000);

and

$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
$ts = substr($xml->a, 0, -3);
echo date('Y-m-d H:i:s', $ts);

both print 2010-03-20 07:38:43 (on my Europe/Berlin machine)

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