user1894929
user1894929

Reputation: 199

call php function every 10mins for 1hour

got this code that should check if url is available and also give me ping:

<?php
function test ($url){
$starttime = microtime(true);
$valid = @fsockopen($url, 80, $errno, $errstr, 30);
$stoptime = microtime(true);
echo (round(($stoptime-$starttime)*1000)).' ms.';

if (!$valid) {
   echo "Status - Failure";
} else {
   echo "Status - Success";
}
}

test('google.com');
?>

How do i make it so this function would be called every lets say 10minues for 1hour (6times in total) ?

Upvotes: 0

Views: 5287

Answers (4)

Sarmad Nazir
Sarmad Nazir

Reputation: 23

Send Email every day in a Month. I used it in daily backup mail..

<?php 
  ignore_user_abort(true); //if page is closed its make it live
  for($i=0;$i<=30;$i++)
  {
     $to = "[email protected]";
     $subject = "Test mail $i";
     $message = "Hello! This is a simple email message.";
     $from = "[email protected]";
     $headers = "From:" . $from;
     mail($to,$subject,$message,$headers);
     sleep(24*60);//send mail in every 24 hour. 
}

?>

Upvotes: 0

Alvaro
Alvaro

Reputation: 41595

I would recommend you to make use of cron jobs if you are using a Unix server or Windows Task Scheduler in the case of windows.

Like this you will be able to use programmed tasks.

In the case of using cron you could do it easily like this:

*/10 * * * *  php -f your_relative_or_full_path_URL/params > /dev/null 

Upvotes: 1

Butt4cak3
Butt4cak3

Reputation: 2429

Use a simple for-loop and the sleep command:

for ($i = 0; $i < 6; $i++) { // Run the code 6 times
    test($url);
    sleep(10 * 60); // Sleep 10 Minutes
}

Upvotes: 1

Naryl
Naryl

Reputation: 1888

depends: do you want to do that just once?

Then you can call the function inside a loop and use sleep() after each execution of the loop.

Do you want to do it everyday? or one specific day each week/month? use a cron.

Upvotes: 1

Related Questions