呃..奇怪...但當然,如果你認爲這是很重要...
,如果你認爲你可以得到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));
}
你不這樣做,至少不是那樣的。 PHP和Javascript不會在相同的環境中運行。你可以用php中的'microtime'來測量執行時間。 – datasage 2013-02-11 15:56:14
你可能是指'new Date()'而不是'new date()'? – Christoph 2013-02-11 15:57:21
所有我想要的是找出一種方法來獲得在while循環內的當前時間流逝的時間值。這個怎麼做? – coder101 2013-02-11 15:57:29