简体   繁体   English

如何找到作为 PHP 响应获得的两个日期之间的差异? 我将响应日期存储在两个 javascript 变量中

[英]How to find difference between two dates obtained as a response from PHP? I'm storing the response dates in two javascript variables

function chatbox($to_user_id,to_name,from_name,$from_user_id,$user_last_activity) 
{
    var to_user_id = $to_user_id;
    var from_user_id = $from_user_id;
    var status = "";
    var last_seen = $user_last_activity;
    var current_time = "<?php
                          echo $current_timestamp = date("Y-m-d H:i:s");
                        ?>"; 
}

I am fetching $user_last_activity from database using php and passing it to the javascript function chatbox.我正在使用 php 从数据库中获取 $user_last_activity 并将其传递给 javascript 函数聊天框。 And i am getting the current time from php date function.我从 php 日期函数获取当前时间。 How to compare these two javascript variable to get the time difference in sec, min and hour.如何比较这两个 javascript 变量以获取以秒、分钟和小时为单位的时差。

Please use php as请使用 php 作为

<script>
var date= "<?php echo $current_timestamp = date('Y-m-d H:i:s'); ?>";
</script>

Remember you can't use " in ' ' string and ' in " " string because it will consider the string end point.请记住,您不能" in ' '字符串中使用" in ' '' in " "字符串中使用' in " "因为它会考虑字符串的终点。

Once you fix the quote issue, there is another issue here:修复报价问题后,这里还有另一个问题:

$current_timestamp = date("Y-m-d H:i:s")

That will produce a timestamp in the format (using moment.js tokens) YYYY-MM-DD HH:mm:ss (eg 2020-03-08 16:38:21), which is not supported by ECMA-262.这将产生格式(使用 moment.js 令牌)YYYY-MM-DD HH:mm:ss(例如 2020-03-08 16:38:21)的时间戳,ECMA-262 不支持该格式。 You don't show how the timestamp is converted to a Date, but I'll guess you're using the built–in parser like:您没有显示时间戳如何转换为日期,但我猜您正在使用内置解析器,例如:

var d = new Date(current_time);

However, since parsing of unsupported formats is implementation dependent, some browsers will return an invalid date.但是,由于不支持格式的解析取决于实现,因此某些浏览器将返回无效日期。

Also, the PHP code produces a "local" date for the server, presumably it's set to UTC.此外,PHP 代码为服务器生成一个“本地”日期,大概它设置为 UTC。 Where browsers do parse a date in the format YYYY-MM-DD HH:mm:ss, it will almost certainly be as local to the browser's host system so will represent a different moment in time from the initial timestamp if the two systems have different settings.浏览器确实解析格式为 YYYY-MM-DD HH:mm:ss 的日期,它几乎肯定是浏览器主机系统的本地时间,因此如果两个系统具有不同的时间,它将代表与初始时间戳不同的时刻设置。

The OP doesn't say what format $user_last_activity is. OP 没有说明$user_last_activity是什么格式。 Presumably it's another timestamp, so it will have the same issues with parsing.大概是另一个时间戳,因此解析时会遇到相同的问题。

Once the above issues are sorted, you can get the difference in milliseconds between two dates by simply subtracting one from the other, see Difference between dates in JavaScript .对上述问题进行排序后,您只需将一个日期与另一个日期相减即可获得两个日期之间的毫秒差异,请参阅JavaScript 中的日期差异 The time difference can then be converted to days, hours, minutes, whatever.然后可以将时差转换为天、小时、分钟等。

I have solved it by changing the logic.我已经通过改变逻辑解决了它。 I just used strtotime() to both $user_last_activity and $current_time in the php page from where i'm calling JavaScript chatbox() function and find the difference there and then passing the difference in the function.我只是将 strtotime() 用于 php 页面中的 $user_last_activity 和 $current_time ,从中我调用 JavaScript chatbox() 函数并在那里找到差异,然后传递函数中的差异。

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

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