呃..奇怪......但当然如果你认为这很重要......
如果您认为可以在 php 脚本仍在运行任何代码的同时将 js 时间从客户端返回到您的客户端,那么它将不起作用!
但是如果需要,您可以通过 ajax 获取信息。
我们开始吧:将 sql 查询中的时间值添加到呈现网站的 DOM 对象中javascript 的值)
为了让事情变得轻松(对我来说),我假设要实现 jQuery。
标头
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));
}