dc_jt Posted February 1, 2008 Share Posted February 1, 2008 Hi Im having a problem where I have a nested loop and an if else statement inside. Basically I have the next five days in an array and I also have an array of unavailable days (taken from database). So at the moment Ive got an array of: Next five days: 2008-02-02 00:00:00 2008-02-03 00:00:00 2008-02-04 00:00:00 2008-02-05 00:00:00 2008-02-06 00:00:00 And an array of the unavailable days: 2008-02-02 00:00:00 2008-02-03 00:00:00 Now I want to only show the AVAILABLE days, therefore eliminating the 2008-02-02 00:00:00 and 2008-02-03 00:00:00. I tried by looping through all days then within this looping through all unavailable days and if a day equals an unavailable day then put this in an array called $aUnavailableDays else put it in array called $aAvailable days but this doesnt seem to work. Please look at my code below: //Available collection dates //Todays date $today = mktime(0, 0, 0, date("m"), date("d"), date("y")); //echo "Today is ".date("d/m/y", $today)."<br />"; //Tomorrows date $tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); $tomorrow = date("Y-m-d 00:00:00", $tomorrow); //2nd day $second_day = mktime(0, 0, 0, date("m"), date("d")+2, date("y")); $second_day = date("Y-m-d 00:00:00", $second_day); //3rd day $third_day = mktime(0, 0, 0, date("m"), date("d")+3, date("y")); $third_day = date("Y-m-d 00:00:00", $third_day); //4th day $fourth_day = mktime(0, 0, 0, date("m"), date("d")+4, date("y")); $fourth_day = date("Y-m-d 00:00:00", $fourth_day); //5th day $fifth_day = mktime(0, 0, 0, date("m"), date("d")+5, date("y")); $fifth_day = date("Y-m-d 00:00:00", $fifth_day); //unavailable days = 2nd & 3rd //Check against holidays $aHolidays = $oTblHolidays->getHolidays($iStoreId); print_r($aHolidays); $aDays = array($tomorrow, $second_day, $third_day, $fourth_day, $fifth_day); $aNonAvailableDays = array(); $aAvailableDays = array(); $iNum = 0; foreach($aDays as $oDay){ foreach($aHolidays as $oHoliday){ if($oHoliday['date'] == $oDay){ //$aNonAvailableDays[] = $oDay; array_push($aNonAvailableDays, $oHoliday['date']); } else { //$aAvailableDays[] = $oDay; array_push($aAvailableDays, $oHoliday['date']); } } $iNum++; } //print_r($aAvailableDays); //print_r($aNonAvailableDays); Is there a better way to do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88892-if-statement-within-nested-loop/ Share on other sites More sharing options...
kenrbnsn Posted February 1, 2008 Share Posted February 1, 2008 Here's one way: <?php $dates = array(); for ($i=0;$i<5;$i++) $dates[] = date('Y-m-d 00:00:00',strtotime('+' . $i . ' days')); $holidays = array('2008-02-02 00:00:00','2008-02-03 00:00:00'); $avail = array_diff($dates,$holidays); echo '<pre>' . print_r($avail,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/88892-if-statement-within-nested-loop/#findComment-455321 Share on other sites More sharing options...
dc_jt Posted February 1, 2008 Author Share Posted February 1, 2008 Thanks Ken, that works great Quote Link to comment https://forums.phpfreaks.com/topic/88892-if-statement-within-nested-loop/#findComment-455328 Share on other sites More sharing options...
laffin Posted February 1, 2008 Share Posted February 1, 2008 does yer script force u to use 2 arrays for available and unavailable. seems like a waste if it does, when u can have the dates, with flags. A multi-dimensional array. which wud cut down on the code, u may also want to keep things in time() format as that makes comparisons much simpler. $today=strtotime("Y-m-D"); for($i=0;$i<5;$i++) $fdays[strtotime("+$i days",$today)]=true; than get yer unavailable days, in the array. then mark the days which are unavailble for($udays as $day) { $dt = strtotime($day); if(isset($fdays[$dt])) $fdays[$dt]=false; } now the array $fdays, have a value of true or false true = available false = unavaliabe foreach($fdays as $key => $value) { echo date("Y-m-d,$key) . " is " . ($value ? "":"un") . "available"; } Quote Link to comment https://forums.phpfreaks.com/topic/88892-if-statement-within-nested-loop/#findComment-455335 Share on other sites More sharing options...
dc_jt Posted February 1, 2008 Author Share Posted February 1, 2008 Here's one way: <?php $dates = array(); for ($i=0;$i<5;$i++) $dates[] = date('Y-m-d 00:00:00',strtotime('+' . $i . ' days')); $holidays = array('2008-02-02 00:00:00','2008-02-03 00:00:00'); $avail = array_diff($dates,$holidays); echo '<pre>' . print_r($avail,true) . '</pre>'; ?> Ken Infact I havent got it exactly how I want it. Please see my code below: $aHolidays = array(); $rHolidays = $oTblHolidays->getHolidays($iStoreId); foreach($rHolidays as $oHoliday){ array_push($aHolidays, $oHoliday['date']); } //Loop through next 5 days and compare with holidays $dates = array(); for ($i=0;$i<5;$i++) $dates[] = date('Y-m-d 00:00:00',strtotime('+' . $i . ' days')); $avail = array_diff($dates,$aHolidays); $aAvailableDays = ($avail); //echo '<pre>' . print_r($avail,true) . '</pre>'; print_r($aAvailableDays); How can I also use the id of each available day? At the moment Ive only got the date field? If I push $oHoliday instead of $oHoliday['date'] then I cant use array_diff can I??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88892-if-statement-within-nested-loop/#findComment-455359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.