Shruti
Shruti

Reputation: 13

date function in while loop of php

I want to use date() function in while loop of PHP but I'm getting an error as

Fatal error: Maximum execution time of 500 seconds exceeded.

I tried to set the execution time more than the time required for the loop to complete, yet it isn't working as expected.

<?php
 ini_set('max_execution_time',500);

 print date("H:i");

 while(date("H:i")!="16:50"){

 $i=0;
 }

if(date("H:i")=="16:50"){

$file = "buttonStatus.txt";
$handle = fopen($file,'w+');
$onstring = "ON";
fwrite($handle,$onstring);
fclose($handle);

$i=1;
} 

echo $i;
?>

Upvotes: 0

Views: 60

Answers (1)

Scriptable
Scriptable

Reputation: 19750

This code:

while(date("H:i")!="16:50"){
   $i=0;
}

Does nothing but create an infinite loop, there is no way to exit it unless the time reaches the set time. It will literally just loop constantly and never reach the code below it.

If you want to run this script at 16:50 everyday you would be better using a cron task/job. A cron is a task that is run repeatedly, it could be every few minutes, every week, month.. whatever you need.

If you can explain what you are trying to achieve here I can give you a better solution.

Upvotes: 0

Related Questions