i have a main 'for' loop and within it i have 4 'while' loops. each sub loop compares a date ( 1 to 4 ) with an $OKdate and makes sure that the OKdate is not before any of the 4 dates, and if the OKdate is then it will add a day (864000 seconds) to the OKdate. since the dates may vary i want to make sure that all dates work. so my question is this one: from the example below am i on the right track if not please HELP, and two how do i get these 4 loops to go back and check all of them until they all work and then end the loop and continue?
1) The initial for() loop is not valid syntax and is actually not the way to do a loop like this. You should use a do-while -
PHP Code:
$exit_flag == 0;
do {
// your logic changes the $exit_flag to a non-zero value when it is done
} while ($exit_flag = 0);
2) From your description, it sounds like you want to increment $OKdate by a day until it is just greater than the highest $datex variable ("makes sure that the $OKdate is not before any of the 4 dates" - which I read to mean, do this until it is higher than all of the 4 dates.) Let us know if this statement is correct and someone can come up with code to do this.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Just in case statement #2 is true, here is some code to do this -
PHP Code:
// generate some dates
$date1 = time() + rand(0, 9999999); // some random future date in time
$date2 = time() + rand(0, 9999999); // some random future date in time
$date3 = time() + rand(0, 9999999); // some random future date in time
$date4 = time() + rand(0, 9999999); // some random future date in time
$OKdate = time() + rand(0, 9999999); // some random future date in time
// determine the maximum
$max_date = max($date1, $date2, $date3, $date4);
// let us see the starting values
echo "d1 = $date1, d2 = $date2, d3 = $date3, d4 = $date4, max = $max_date, OKdate = $OKdate<br>";
// reset the exit flag
$exit_flag = 0;
// loop until the flag is set to a 1
do {
if($OKdate < $max_date){
// value < maximum, add one day
$OKdate += 864000;
echo "adding a day, OKdate = $OKdate<br>";
} else {
// value >= maximum, done, set the exit flag
$exit_flag = 1;
echo "done<br>";
}
} while ($exit_flag == 0);
Or more simply (since there is only one thing going on inside the loop, and with some additional delta info) -
PHP Code:
// generate some dates
$date1 = time() + rand(0, 9999999); // some random future date in time
$date2 = time() + rand(0, 9999999); // some random future date in time
$date3 = time() + rand(0, 9999999); // some random future date in time
$date4 = time() + rand(0, 9999999); // some random future date in time
$OKdate = time() + rand(0, 9999999); // some random future date in time
// determine the maximum
$max_date = max($date1, $date2, $date3, $date4);
// let us see the starting values
echo "d1 = $date1, d2 = $date2, d3 = $date3, d4 = $date4, max = $max_date, OKdate = $OKdate<br>";
while ($OKdate < $max_date){
$delta = $max_date - $OKdate;
$OKdate += 864000;
echo "OKdate is $delta less than the highest date, adding a day, OKdate is now = $OKdate<br>";
}
$delta = $OKdate - $max_date;
echo "done, OKdate is $delta more than the highest date<br>";