2013-02-11 59 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之後?或者我可以在