3

Is it possible in PHP to display any looping with in a interval of time For Example :- I like to display 1 after 5 minute it will display 2. Any help will be greatfull. Thank you in advance.

sameer kumar
  • 149
  • 2
  • 3
  • 13
  • check this link ...http://www.w3schools.com/php/func_misc_sleep.asp – amit_183 Nov 04 '14 at 11:58
  • 2
    You could use `sleep`, but, see how it works. The program will execute it, but, will just show you the result, after time passes. It does, at it says, "Run, sleep till time X, wake and show". Any other way, Javascript/Ajax. – klauskpm Nov 04 '14 at 12:01
  • You should use cron jobs http://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – Anri Nov 04 '14 at 12:07

4 Answers4

0

Cron Job sample

#!/usr/bin/env php
<?php
# This file would be say, '/usr/local/bin/run.php'
// code
echo "this was run from CRON"

Then, add an entry to the crontab:

* * * * * /usr/bin/php -f /usr/local/bin/run.php &> /dev/null

Source : https://stackoverflow.com/a/18737637/3793639

Community
  • 1
  • 1
Anri
  • 1,706
  • 2
  • 17
  • 36
0

If you need this in a long time running php script (loop), then you can use time() to get the starttime of each interval.

$start = time();
$cnt = 0;
while (1) # your loop
{
  $now = time();
  if($start +(5*60)>=$now)
  {
     $start = $now;
     $cnt++;
     echo $cnt;
  }

}
Frank
  • 1,901
  • 20
  • 27
-1
foreach (range(1, 10) as $number) {
    echo $number;
    sleep(60);
}

here

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
litechip
  • 326
  • 2
  • 9
-1

Maybe combine it with flush() that's what you need test it.

if (ob_get_level() == 0) ob_start();

for ($i = 0; $i<10; $i++){

        echo "<br> Line to show.";
        echo str_pad('',4096)."\n";    

        ob_flush();
        flush();
        sleep(2);
}

echo "Done.";

ob_end_flush();

?>
Kavvson
  • 825
  • 3
  • 9
  • 23
  • This is working but when a user open his/her website he has to wait 2second or if haigher may be 24 hour than how he ll do?? – sameer kumar Nov 04 '14 at 12:14
  • In that case simple sleep wont help you out here[cause you need to have the page to be constantly opened] . You need to use cron for running script in "background" thought you need something simpler. Cron under windows xampp if you need http://stackoverflow.com/questions/25236484/php-cron-job-on-xampp-windows – Kavvson Nov 04 '14 at 12:16