Reputation: 1605
i have a page where i'm trying to loop through the results from mysql query and in this block i need to send the JS new date().getTime()
value to a php function for calculation of time elapsed, all inside the while loop.
how do i achieve this?
my php page is like :
<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>
EDIT Maybe i have confused people with trying to figure out execution time which is not the case. i want the time value from mysql query to be compared with client machine's time from JS in a php function. my php function calculates the time elapsed.
Upvotes: 0
Views: 665
Reputation: 811
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));
}
Upvotes: 1
Reputation: 19563
If you want to measure execution time within PHP, you need to use microtime
;
$start = microtime(true);
// Some code to measure
$time = microtime(true) - $start;
Upvotes: 0
Reputation: 785058
You should better use PHP's time function like this:
$currTime = time();
However if there is a case where client browser and your web server can be in different timezones then you can pass javascript time to your PHP script using a get query parameter and then access it inside PHP script like:
$currTime = $_GET['jsCurrTime'];
Upvotes: 0