• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

If statement hangs script

I'm using the following code to select dates by comparing the week day to a selected checkbox. The values of the checkboxes are stored in an array and the values are 0 for Sunday, 1 for Monday, etc. So if Monday, Wednesday and Friday are selected, the dates that fall into those days from a date range will get printed out. However, when I add the if statement to compare the date with the week number, the script hangs on and doesn't finish. If I take out the if statement, all the days that fall between the range are printed out. Do I have an error?

$compare_date = $press_date;
foreach($myarray as $value)
{
  while ($press_date <= $recurrence_stop)
  {
    if(date('w',strtotime($press_date)) == $value)
    {
      echo $press_date;echo '-';echo date('w',strtotime($press_date));echo '-'; echo $value;echo '<br />';
      $press_date = date('Y-m-d',strtotime("+1 day, $press_date"));
   }
} //Closes while
$press_date = $compare_date;
}//Closes foreach

The posted values are $press_date and $recurrence_stop. I store the original $press_date into another variable so it can be reassigned to the variable $press_date. Thanks!
0
horalia
Asked:
horalia
  • 2
1 Solution
 
horaliaAuthor Commented:
I even used a strcmp, that didn't work either.
0
 
steelseth12Commented:
move  $press_date = date('Y-m-d',strtotime("+1 day, $press_date")); out of the if statement.

$compare_date = $press_date;
foreach($myarray as $value)
{
  while ($press_date <= $recurrence_stop)
  {
    if(date('w',strtotime($press_date)) == $value)
    {
      echo $press_date;echo '-';echo date('w',strtotime($press_date));echo '-'; echo $value;echo '<br />';
     
   }
    $press_date = date('Y-m-d',strtotime("+1 day, $press_date"));
} //Closes while
$press_date = $compare_date;
}//Closes foreach
0
 
nplibCommented:
Try this, this is a snippet of a function I made to accomplish a similar task.

You will have to change your check box values from 0, 1, 2, 3 etc. to the Full name of the day of week.
Monday, Tuesday, Wednesday etc..

<?php
$new_array_days_of1 = array();
$new_array_days_of2 = array();
$new_array_days_of = array();
$endofRange = "12-31-2007";
$beginofRange = "12-01-2007";
$timestamp = strtotime($beginofrange);
$UNC_start_date = date("U", $timestamp);
$UNC_start_date_org = $UNC_start_date;


$array_days_of = array("Monday", "Wednesday", "Friday");   //get this value from the checkbox

$new_array_days_of1 = array_slice($array_days_of, array_search(date("l", $UNC_start_date), $array_days_of));
$new_array_days_of2 = array_slice($array_days_of, 0, array_search(date("l", $UNC_start_date), $array_days_of));
$new_array_days_of = array_merge($new_array_days_of1, $new_array_days_of2);
if (count($new_array_days_of) >= 2) {
      for ($z=0; $z<=count($new_array_days_of)-1; $z++) {
            $array_rec_dates = array();
            if ($z > 0) {
                  $UNC_start_date = strtotime("last ".$new_array_days_of[$z], $UNC_start_date);
            }
            if ($z == 0) {
                  array_push($array_rec_dates, $UNC_start_date);
            }
            $r = strtotime("next ".$new_array_days_of[$z], $UNC_start_date);
            $ri = ($r-$UNC_start_date);
            do {
                  if ($r >= $UNC_start_date_org) {
                        array_push($array_rec_dates, $r);
                  }
                  $r = ($r + $ri);
            } while ($r <= $endofRange);
            sort($array_rec_dates);
            if ($rr_exception_date) {
                  for ($x=0; $x<=count($array_UNC_exception_date); $x++) {
                        for ($y=0; $y<=count($array_rec_dates); $y++) {
                              if (($array_UNC_exception_date[$x] == $array_rec_dates[$y]) && ($array_UNC_exception_date[$x]) && ($array_rec_dates[$y])) {
                                    unset($array_rec_dates[$y]);
                              }
                        }
                  }
                  $session_dates .= date("D. ", $array_rec_dates['0']);
                  foreach ($array_rec_dates as $ard) {
                        $session_dates .= date("M., j, Y", $ard)." - ";
                  }
                  $session_dates = rtrim($session_dates, " - ");
                  $session_dates .= "<br />";
            } else {
                  $session_dates .= date("D. M., j, Y", $array_rec_dates['0'])." - ".date("M., j, Y", end($array_rec_dates))."<br />";
            }
      }
} else {
      $array_rec_dates = array();
      array_push($array_rec_dates, $UNC_start_date);
      $r = strtotime("next ".$array_days_of['0'], $UNC_start_date);
      $ri = ($r-$UNC_start_date);
      do {
            if ($r >= $UNC_start_date_org) {
                  array_push($array_rec_dates, $r);
            }
            $r = ($r + $ri);
      } while ($r <= $endofRange);
      sort($array_rec_dates);
      if ($rr_exception_date) {
            for ($x=0; $x<=count($array_UNC_exception_date); $x++) {
                  for ($y=0; $y<=count($array_rec_dates); $y++) {
                        if (($array_UNC_exception_date[$x] == $array_rec_dates[$y]) && ($array_UNC_exception_date[$x]) && ($array_rec_dates[$y])) {
                              unset($array_rec_dates[$y]);
                        }
                  }
            }
            $session_dates .= date("D. ", $array_rec_dates['0']);
            foreach ($array_rec_dates as $ard) {
                  $session_dates .= date("M., j, Y", $ard)." - ";
            }
            $session_dates = rtrim($session_dates, " - ");
            $session_dates .= "<br />";
      } else {
            $timestamp_b = strtotime($evr_event_date);
            $timestamp_e = strtotime($endofRange);
            $session_dates = date("D. M., j, Y", $timestamp_b);
            $session_end_dates = date("M., j, Y", $timestamp_e);
      }
}

echo $session_dates;
echo $session_end_dates;
?>
0
 
horaliaAuthor Commented:
I forgot to post an answer last Friday, steels answer worked. I'll try your suggestion as well nplib and close the question later in the day. Thanks!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now