Uh.. strange... but of course if you think this is important...
It will NOT work if you think you can get the js-time back from the client to your WHILE the php script is still running any code!
But you can get the information via ajax if you want.
here we go:
add the time value from your sql query to a DOM object of the rendered website
most simple by adding a javascript var directly (you could also use hidden or even visible DOM objects of your choice as long as you read the correct objects value with javascript)
To ease things up (for me) I'm assuming jQuery to be implemented.
javascript header
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.
And the php should have a trigger for the ajax request put this into your php, as far up as possible (but before anything gets echoed or queried to your 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));
}
microtimein php.new Date()instead ofnew date()?