2013-02-11 60 views
0

我有一个页面,我试图通过mysql查询的结果循环,并在此块中,我需要发送JS new date().getTime()值到一个PHP函数计算所用时间,所有内部while循环。发送JavaScript的时间戳到PHP函数进行计算

我该如何做到这一点?

我的PHP页面是这样的:

<body> 
<?php 
while($rows = $result->fetch_assoc()){ 
echo "<div>".// now here i want to send the time value from JS to function 
like time($a JS value)."</div>" 
    } 
    ?> 

</body> 

编辑也许我有困惑的人试图找出执行时间是不是这样的。我想从MySQL的查询时间值与客户端机器的时间从一个PHP函数中的JS进行比较。我的php函数计算已用时间。

+1

你不这样做,至少不是那样的。 PHP和Javascript不会在相同的环境中运行。你可以用php中的'microtime'来测量执行时间。 – datasage 2013-02-11 15:56:14

+0

你可能是指'new Date()'而不是'new date()'? – Christoph 2013-02-11 15:57:21

+0

所有我想要的是找出一种方法来获得在while循环内的当前时间流逝的时间值。这个怎么做? – coder101 2013-02-11 15:57:29

回答

0

你应该更好地使用PHP的时间函数是这样的:

$currTime = time(); 

不过,如果有就是客户端浏览器和Web服务器可以在不同的时区的情况下,那么你可以使用通过JavaScript的时间,你的PHP脚本一大家子查询参数,然后访问它的PHP脚本中,如:

$currTime = $_GET['jsCurrTime']; 
+0

但我想检查从JS的客户端的时间与从MySQL查询返回的时间进行比较。我不能使用php当前时间因为服务器将在不同的时区 – coder101 2013-02-11 16:01:16

+0

@ coder101:请看我完整的答案,我仍然在键入它:) – anubhava 2013-02-11 16:01:55

+0

我编辑了我的问题,请检查,我认为我已经做到了这一次更清楚。你现在想什么? – coder101 2013-02-11 16:07:28

0

如果你想测量内PHP执行时间,你需要使用microtime;

$start = microtime(true); 
// Some code to measure 
$time = microtime(true) - $start; 
+0

我编辑了我的问题,请检查,我认为我已经更清楚了这次 – coder101 2013-02-11 16:06:55

1

呃..奇怪...但当然,如果你认为这是很重要...

,如果你认为你可以得到JS-时间回从客户端将无法正常工作的尽管PHP脚本仍在运行任何代码!

但是,如果你愿意,你可以通过ajax获取信息。

在这里我们去: 从SQL查询中直接添加一个JavaScript变种增加时间价值的渲染网站的DOM对象 最简单的(你也可以用你只要选择隐藏或甚至可见的DOM对象当你阅读正确的对象值与JavaScript)

为了缓解(对我来说)我假设jQuery的实施。

JavaScript的头

var sqltime = 1360599506; // unix timestamp set by php 
// now let's get the information from the user incl his timezone 
var tnow = new Date(); // string something like: "Mon Feb 11 2013 17:24:06 GMT+0100" 
// and a timestamp-like number for tnow 
var tstmp = var now = Math.round(tnow.getTime()/1000) 
//and now send those three values via ajax to the server: 
var postdata = {action: 'ajaxtime', ts: sqltime, tj: tstmp, tr: tnow}; 
$.ajax({ 
    type: 'POST', 
    url: "http://example.com/my.php", 
    data: postdata, 
    success: function (response) 
    { 
     //do something with the response if you want. just decode the received json string if any 
    } 
}); 
//you could also compare the two timestamps on clientside if this is more convenient. 

,而PHP的应该有一个Ajax请求把这个变成你的php触发,尽可能向上可能(但在此之前得到任何回应或查询到您的SQL!)

if (array_key_exists('ajaxtime', $_REQUEST)) 
{ 
    $sql time = $_POST['ts']; 
    $js_timestamp = $_POST['tj']; 
    $readable_js_time = $_POST['tr']; 

// here you can calculate the timestamps and get timezone from the readable_js_time and do whatever you need to. 
$json['success'] = true; 
$json['result'] = "my result from the calculation"; 

    // make sure no other code of this php is executed because of the ajaxtime request 
    die(); 
    //if you want to response with a json string to parse in the javascript response use this: 
    //die (jsonEncode($json)); 
} 
+0

您提供的php代码是否放在了js请求的页面上?并且你在ajax请求中给出的那个,即my.php应该是一个单独的请求? – coder101 2013-02-12 07:11:41

+0

应该将JS放在evrything之上,但在php之后?或者我可以在