Link to home
Start Free TrialLog in
Avatar of Kevin Cranford
Kevin Cranford

asked on

PHP function looping when it should not be.

This isn't very critical since it is allowing me to still get the result I am looking for, but it still bothers me.

I have the following php function (the $runnumber and echo statements were added to follow the flow, they will be removed):
function verifydate($dueDate,$length,$runnumber) {
  echo "Run number: " . $runnumber . "<br>";
  echo "Current due date: " . $dueDate . "<br>";
  $dayName = strftime("%A", strtotime("$dueDate"));
  echo "Current day of week: " . $dayName . "<br>";
  if ($dayName == "Saturday") {
    $length = $length + 2;
    $dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
    echo "Since the day is Saturday, adding 2 more days to the due date and checking again.<br><br>";
    $runnumber = $runnumber + 1;
    verifydate($dueDate,$length,$runnumber);
  }
  if ($dayName == "Sunday") {
    $length = $length + 1;
    $dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
    echo "Since the day is Saturday, adding 1 more day to the due date and checking again.<br><br>";
    $runnumber = $runnumber + 1;
    verifydate($dueDate,$length,$runnumber);
  }
  $conn2 // a connection to my database.
  $sql = "SELECT dayID FROM excludedDays WHERE excludedDay = '$dueDate'";
  $result = $conn2->query($sql);
  echo "Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:<br>";
  echo $sql . "<br>";
  echo "Which returned " . $result->num_rows . " result.<br>";
  if ($result->num_rows > 0) {
    $length = $length + 1;
    $dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
    $runnumber = $runnumber + 1;
    echo "Since the date was excluded the new due date is: " . $dueDate . "<br><br>";
    verifydate($dueDate,$length,$runnumber);
  }
  return $dueDate;
}
$dateDue = verifydate("2017-09-16", 1, 1);
echo "<br>The final due date is: " . $dateDue . "<br>";

Open in new window


The problem is that the results of this are as follows:
Run number: 1
Current due date: 2017-09-16
Current day of week: Saturday
Since the day is Saturday, adding 2 more days to the due date and checking again.

Run number: 2
Current due date: 2017-09-18
Current day of week: Monday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-18'
Which returned 1 result.
Since the date was excluded the new due date is: 2017-09-19

Run number: 3
Current due date: 2017-09-19
Current day of week: Tuesday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-19'
Which returned 0 result.
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-18'
Which returned 1 result.
Since the date was excluded the new due date is: 2017-09-19

Run number: 3
Current due date: 2017-09-19
Current day of week: Tuesday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-19'
Which returned 0 result.

The final due date is: 2017-09-19

As you can see in run number 3 it has used the correct day, which is not an excluded day, and returned 0 results. At that point, in line  26 of the code it should have failed the if statement and went straight to the return $dueDate on line 33. But instead, it runs again using the date it was prior to the increase, what it was in run number 2, and then runs it again and successfully returns the correct date.

While I am getting the correct result I am concerned that whatever is causing this could create an issue in the future. Any help would be appreciated. Also if you know of a more elegant solution to handle this it would be appreciated as well.
Avatar of Kevin Cranford
Kevin Cranford

ASKER

This problem is getting stranger. Just to see what would happen with a different value for $length I set it to 7 and added the entirety of the following week to the exludedDays. I even added another echo to let me know when all the checks have failed, basically bringing me to the return statement.

The code I added just prior to the return statement is:
echo "The due date that should be returned is: " . $dueDate . "<br>";

Open in new window

My result is:
Run number: 1
Current due date: 2017-09-22
Current day of week: Friday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-22'
Which returned 1 result(s).
Since the date was excluded the new due date is: 2017-09-23

Run number: 2
Current due date: 2017-09-23
Current day of week: Saturday
Since the day is Saturday, adding 2 more days to the due date and checking again.

Run number: 3
Current due date: 2017-09-25
Current day of week: Monday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-25'
Which returned 0 result(s).
The due date that should be returned is: 2017-09-25
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-25'
Which returned 0 result(s).
The due date that should be returned is: 2017-09-25
The due date that should be returned is: 2017-09-23

The final due date is: 2017-09-23

Open in new window

As you can see in line 20 in echoed the string correctly and should have then hit the return statement and left the function. Instead, it ran again, matched the correct date again, echoed the string again, then somehow skipped everything and took two days off of the date, echoed that string and then finally hit the return statement. Which in this case, it gave me the wrong date because it should never return a date that is a Saturday.
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cranford
Kevin Cranford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial