比较 Coldfusion 和 PHP 时间戳(以毫秒为单位)

发布于 2024-12-20 04:06:25 字数 791 浏览 4 评论 0原文

我正在使用自定义函数在 CF8 中创建时间戳。然后它通过查询字符串传递到 PHP 并再次与当前时间戳进行比较(使用 time())。由于 CF8 没有内置的 DateDiff() 毫秒选项,因此我在返回值(以秒为单位)的末尾附加了 3 个零。

但是,当我将其与 PHP 时间戳进行比较时,差异应该以秒为单位,但我得出了几个小时的差异。

<cfscript>
function GetEpochTime() {
    datetime = Now();
    return DateDiff("s", "January 1 1970 00:00", datetime) & "000"; //zeroes padded for milliseconds
}
</cfscript>
<cfset foo = getepochtime() /> //Returns time stamp

为了方便讨论,CF 时间戳为 1323344375000。与 PHP 的时间戳值为 1323362710000 相比,差异为 18,555,000 毫秒(超过 300 分钟)。实际过去的时间可能是2秒。

$php_ts = time() * 1000; //PHP timestamp is in seconds, too
$cf_ts = $_GET['coldfusion_timestamp'];
echo $ts_difference = ($php_ts - $cf_ts) / (1000 * 60); //Difference in minutes

我的转换失败在哪里?

I'm creating a timestamp in CF8 using a custom function. It's then being passed via query string to PHP and compared, again, to the current timestamp (using time()). Since CF8 doesn't have a built-in millisecond option for DateDiff(), I appended 3 zeroes to the end of the returned value (which is in seconds).

But, when I compare it against the PHP timestamp, the difference should be in seconds, but I'm coming up with a multi-hour difference.

<cfscript>
function GetEpochTime() {
    datetime = Now();
    return DateDiff("s", "January 1 1970 00:00", datetime) & "000"; //zeroes padded for milliseconds
}
</cfscript>
<cfset foo = getepochtime() /> //Returns time stamp

For the sake of argument, the CF timestamp reads 1323344375000. When compared against PHP's stamp with a value of 1323362710000, the difference is 18,555,000 milliseconds (over 300 minutes). Real time that has passed is maybe 2 seconds.

$php_ts = time() * 1000; //PHP timestamp is in seconds, too
$cf_ts = $_GET['coldfusion_timestamp'];
echo $ts_difference = ($php_ts - $cf_ts) / (1000 * 60); //Difference in minutes

Where is my conversion failing?

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

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

发布评论

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

评论(1

初心未许 2024-12-27 04:06:25

在 GMT 偏移的上下文中,您没有考虑服务器的本地时间。

将 getEpochTime() 修改为:

 return DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), datetime) & "000";

来源:GetEpochTimeFromLocal() (Rob-Brooks Bilson)

You're not taking the server's local time into consideration, within the context of the GMT offset.

Modify your getEpochTime() to:

 return DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), datetime) & "000";

Source: GetEpochTimeFromLocal() (Rob-Brooks Bilson)

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