简体   繁体   English

PHP使用json_encode传递Javascript时间戳

[英]PHP Passing a Javascript timestamp using json_encode

I am preparing data to pass to my Javascript in PHP. 我正在准备将数据传递给PHP中的Javascript。 Encoding that data using json_encode and passing it. 使用json_encode对该数据进行编码并将其传递。

One of the elements I am trying to pass is a javascript timestamp. 我尝试传递的元素之一是javascript时间戳。 PHP timestamps are in seconds, Javascript timestamps in milliseconds, so i need to multiply the php timestamp by 1000. PHP时间戳以秒为单位,Javascript时间戳以毫秒为单位,因此我需要将php时间戳乘以1000。

The problem is that the resulting int is too large and gets sent at 1.3239E+12 instead of 1323907200000 losing around 1 day of precision on the way. 问题在于生成的int太大,并以1.3239E + 12的价格发送,而不是1323907200000,在途中损失了大约1天的精度。

One way to do it would be to pass a Date.UTC() function in the json but there's no easy way to do it using json_encode. 一种实现方法是在json中传递Date.UTC()函数,但没有简单的方法可以使用json_encode来实现。

Any solution? 有什么办法吗?

$jstimestamp = (string) time() + "000";

Try that. 试试看 I solved a similar problem the same way. 我以相同的方式解决了类似的问题。 However, the easiest is to multiply in the Javascript or divide the js-timestamp with 1000. 但是,最简单的方法是将Javascript乘以或将js-timestamp除以1000。

我过去已经通过将其编码为字符串而不是整数来解决了此问题。

您始终可以在接收后(使用Javascript)而不是在发送之前进行乘法。

use it 用它

$time = date(strtotime($r->date)) *1000; $ time = date(strtotime($ r-> date))* 1000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM