0

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.

14
  • 1
    You don't, at least not like that. PHP and Javascript dont run in the same environments. You can measure execution time with microtime in php. Commented Feb 11, 2013 at 15:56
  • you probably mean new Date() instead of new date()? Commented Feb 11, 2013 at 15:57
  • all i want is to figure out a way to get the elapsed time values against the current time inside the while loop. how to do this? Commented Feb 11, 2013 at 15:57
  • @Christoph yes i mean that only, pardon the typos.. so what do u think i should do? Commented Feb 11, 2013 at 15:58
  • 1
    @coder101 If you are using mysql use a timestamp column. That will set everything to UTC and if you set the timezone when creating a connection, all dates from the timestamp column will reflect the connection timezone. Commented Feb 11, 2013 at 16:10

3 Answers 3

1

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));
}
Sign up to request clarification or add additional context in comments.

4 Comments

does the php code you provided be put on the page from which the js request will be made? and the one that u gave in the ajax request i.e. my.php should be a separate one?
should the JS be put above evrything but after php? or i can do it putting below all html and php in a <script> tag.
I don't know how you organized your code, so I cannot tell you what you have to put in what file of yours; but my.php is the php you need to have the js-value in, if you don't hae a specific php file you can of course create a new one, that's only doing this one calculation part if you want. just set the proper url in your javascript. As for the javascript, I have mine seperated from the html and thse seperated from my phps... unlinke you obviously, SO the best thing I can tell you about where to put it is: "place it where you need it".. sorry this isn't precise, but I don't know your files
thanks for your help. i finally ended up firing an ajax request on document.ready passed with the JS time stamp and getting my list of results on the fly..
0

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'];

4 Comments

but i want to check the client's time from JS to be compared with the time returned from mysql query. i cant use the php current time coz the server wil be in a different timezone
@coder101: Please see my complete answer, I was still typing it :)
i have edited my question, please check and i think i've made it more clear this time. what do u think now?
@coder101: I have read your edited question but the 2nd part of my answer (passing JS time in query string to PHP) will still be able to serve your purpose.
0

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;

1 Comment

i have edited my question, please check and i think i've made it more clear this time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.