简体   繁体   English

如何将 php date_diff 值转换为 javascript integer?

[英]How do I convert a php date_diff value to a javascript integer?

I'm trying to use the difference between two dates to help me draw lines on an HTML canvas .我正在尝试使用两个日期之间的差异来帮助我在HTML canvas上画线。 I generate one date in PHP and extract the other which is stored in an SQL database我在PHP中生成一个日期并提取另一个存储在SQL数据库中的日期

When I keep everything in PHP the echo'd output is exactly what I expect.当我将所有内容保存在PHP中时,回显的 output 正是我所期望的。 However, when I json_encode the output, which I expect to be an integer, to use to draw the shape I want on the canvas , a completely different number is presented from javascript . However, when I json_encode the output, which I expect to be an integer, to use to draw the shape I want on the canvas , a completely different number is presented from javascript .

I'm pretty sure it has to do with the json encoding... but I haven't been able to find any reference about how to do this properly.我很确定它与json编码有关......但我无法找到有关如何正确执行此操作的任何参考。

Here's the PHP I use to grab, format, and create a date difference output:这是我用来抓取、格式化和创建日期差异 output 的PHP

$sdt = new DateTime($row['sdate']);
$today = new DateTime('now');

$sdiff = date_diff($sdt, $today);
$sdelta = $sdiff->format("%a");

If I keep all of this in PHP and either echo the variable or echo the json_encode variable I get the expected answer, which is 34.如果我将所有这些都保存在PHP中并echo显变量或echojson_encode变量,我会得到预期的答案,即 34。

echo $sdelta;

results in 34结果 34

echo json_encode($sdelta);

results in "34";结果为“34”;

echo json_decode($sdelta);

results in 34;结果为34;

However when I assign this value to a javascript variable and test the result of the assignment:但是,当我将此值分配给javascript变量并测试分配结果时:

var diffdt = <?php echo json_encode($sdelta); ?>;
alert(diffdt);

The alert popup shows 199. alert弹出窗口显示 199。

If anybody could help me with this issue, I'd be eternally grateful.如果有人可以帮助我解决这个问题,我将永远感激不尽。


Extra information...额外的信息...

  • Column type for sdate is DATE sdate的列类型是DATE
  • query I'm executing is to get the data is:我正在执行的查询是获取数据是:

     $stmt = $link->prepare("SELECT * FROM register WHERE id =? "); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $num = $result->num_rows; $row = $result->fetch_assoc(); var_dump($row['sdate']);
  • Number of rows is 1.行数为 1。
  • vardump result is: string(10) "2019-09-27" vardump 结果为:字符串(10)“2019-09-27”

Must use "Number(your_number)" method I used here,see bottom line:必须使用我在这里使用的“Number(your_number)”方法,见底线:

<script type="text/javascript">

    // To set two dates to two variables 
    var date1 = new Date("06/30/2019"); 
    var date2 = new Date("07/30/2019");

    // To calculate the time difference of two dates 
    var Difference_In_Time = date2.getTime() - date1.getTime();

    // To calculate the no. of days between two dates 
    var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24); 

    console.log(Number(Difference_In_Days));
</script>

Ended up resolving this issue by moving php code outside some of the javascript I had written.最后通过将 php 代码移到我编写的一些 javascript 之外来解决这个问题。 Problem was from a for loop I had implemented that updated javascript values from the php loop.问题来自我已经实现的 for 循环,该循环从 php 循环中更新了 javascript 值。 This was adding up the result.这是把结果加起来。 None of my code actually changed, it was just rearranged.我的代码实际上都没有改变,只是重新排列了。

Didn't think this was an issue since the for loop was doing it's job for what it was designed.不认为这是一个问题,因为 for 循环正在按照它的设计工作。

Thanks everyone for your help.感谢大家的帮助。 It allowed me to eliminate most of the other potential issues.它使我能够消除大多数其他潜在问题。

airider74 When you say that PHP shows the correct data but JS does not, what are you using to display the values-- ie console/terminal/browser etc. ? airider74 当您说 PHP 显示正确的数据但 JS 没有显示时,您使用什么来显示值 - 即控制台/终端/浏览器等? Are they both using UTF8?他们都使用UTF8吗? Something you might try is encoding the output from the PHP via urlencode() and then decode it in JS via decodeURI().您可能会尝试通过 urlencode() 从 PHP 对 output 进行编码,然后通过 decodeURI() 在 JS 中对其进行解码。

PHP: PHP:

echo urlencode(dIffdt);

JS: JS:

var diffdt = "37";  alert( decodeURI(diffdt) );

Or try base64 encoding.或者尝试 base64 编码。

PHP: PHP:

echo base64_encode(diffdt);

JS: JS:

var diffdt = "37";  alert( btoa(diffdt) );

I am thinking possibly your data is ASCII coming out of the server, but JS uses UTF8 by default (as do most modern browsers in a more general sense).我在想你的数据可能是来自服务器的 ASCII,但 JS 默认使用 UTF8(就像大多数现代浏览器一样)。

You need to set the variable without the json_encode, so it would be:您需要设置没有 json_encode 的变量,所以它是:

var diffdt = <?php echo $sdelta; ?>;
alert(diffdt);

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

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