Jump to content

If Statement within nested loop


dc_jt

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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";
}

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.