问题描述:

There is a HTML form in the client browser. The user clicks the submit button. An XHR is sent by JavaScript to the server then. The PHP knows the time of the arrival ($_SERVER['REQUEST_TIME']), but knows nothing about the time of submission. How could it be calculated precisely?

网友答案:

So as suggested, you'd want to add the timestamp to your XHR request as an additional URL parameter:

"?sent_at=" + (new Date()).getTime()

The thing to remember is that javascript's dates are in milliseconds, whereas dates in PHP are measured in seconds - so you might have to convert the timestamp from the browser when you use it in PHP, something like:

$date = intVal($_REQUEST['sent_at']) / 1000;

If you really want millisecond precision, then you can use the submitted value as is - depends what you want to do with it.

Hope this helps!

相关阅读:
Top