0

I want php file's output as it show new line displaying time every second, I've tried using php sleep() function, but didn't get desired result. code i tried is as below.

<?php

echo date('h:i:s').'<br>';
sleep(1);
echo date('h:i:s').'<br>';
sleep(1);
echo date('h:i:s').'<br>';
sleep(1);
echo date('h:i:s').'<br>';
sleep(1);

?>

what is does is..it shows output of 4 lines after total 4 seconds, and what i want is, single line should be added every single second. Is it possible?

P.S: I'm beginner for php

  • sure, i want output same as this code gives. But this code gives output of 4 lines after total 4 seconds, I want output as each time stamp should be added one by one every second. Is it possible? – Jigar Patel Sep 23 '14 at 06:55
  • [Script to read atomic time](http://forum.de.selfhtml.org/archiv/2005/1/t99728/#m610329) http://stackoverflow.com/questions/16592142/retrieve-time-from-ntp-server-via-php – Franz Holzinger Sep 23 '14 at 07:07

2 Answers2

2

This may help you in this

<?php

for( $i = 0 ; $i < 10 ; $i++ )
{
    echo date('h:i:s').'<br>';

    //flush the output buffer
    flush();

    //send the output buffer
    ob_flush();

    //sleep here
    sleep(1);
}
Sundar
  • 4,580
  • 6
  • 35
  • 61
  • yes, it worked for me..I am not able to mark as correct right now because it's only few minutes I've posted question. Will mark it ASAP. I didn't had idea of flush function. Will search and study it soon. Thank you – Jigar Patel Sep 23 '14 at 06:58
  • you can change the i limit to what ever you want this will work. Make infinite loop also will work for you – Sundar Sep 23 '14 at 06:59
  • change the for loop be like this " for (;;) " for infinite loop. When you feel right then accept not to hurry – Sundar Sep 23 '14 at 07:00
  • @Sundar I don't think `ob_flush()` is doing anything here, as far as the OP is concerned. Where is the output buffer started exactly? – Boaz Sep 23 '14 at 07:02
  • @Boaz you may get better answer here http://stackoverflow.com/questions/4191385/php-buffer-ob-flush-vs-flush – Sundar Sep 23 '14 at 07:04
  • @Sundar -1 I'm afraid, as it stands your question is not complete and somewhat misleading. `ob_flush()` should be preceded by an output buffer being initiated. – Boaz Sep 23 '14 at 07:09
  • as per flush() function description "flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those." You have to stop the internal PHP usage this is what you have to use flush(). To send those results to browser you have to use ob_flush(). – Sundar Sep 23 '14 at 07:13
  • @Sundar Again, as it stands your answer contains only code and no explanation whatsoever. Thus using `ob_flush()` without giving the proper context is misleading to future readers who'll think this this is something standard in PHP that should be done in all scenarios. – Boaz Sep 23 '14 at 07:22
  • Added inline comments i guess it's enough nothing there to explain for loop and date functions – Sundar Sep 23 '14 at 07:23
-1

What you are asking is not possible. PHP is a server side script which runs on your server before it renders an result which it then presents to the visitor.

Your code will as you yourself state, run for 4 seconds and then when it is finished with its code, display it to the visitor.

PHP will always run through its script on the server before serving up its result. To achieve dynamically adding elements to an HTML DOM you need to start using Javascript.

Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
  • See @sundar answer. Somehow its possible. – Manwal Sep 23 '14 at 06:58
  • I have sundars answer implemented to this url at the moment, still the same result as I explained above: http://adresseavisen.appcenter.no/test/test.php – Ole Haugset Sep 23 '14 at 07:01
  • this Sundar's code works well at my localhost, it adds single line every second. I don't know is it because of server being local and remote or what. I haven't tried yet on remote server. – Jigar Patel Sep 23 '14 at 07:06