0

I have two arrays :

 $data = Array
(
[0] => Array
    (
        [StartTime] => 13:00:00
        [EndTime] => 14:00:00
    )

[1] => Array
    (
        [StartTime] => 16:00:00
        [EndTime] => 16:30:00
    )
  )

 $slot = Array
 (
   [0] => 09:00:00
   [1] => 09:30:00
   [2] => 10:00:00
   [3] => 10:30:00
   [4] => 11:00:00
   [5] => 11:30:00
   [6] => 12:00:00
   [7] => 12:30:00
   [8] => 13:00:00
   [9] => 13:30:00
   [10] => 14:00:00
   [11] => 14:30:00
   [12] => 15:00:00
   [13] => 15:30:00
   [14] => 16:00:00
   [15] => 16:30:00
   [16] => 17:00:00
   [17] => 17:30:00
)

now $data is my blocked time slots. and $slot is my total time slots. i want to match blocked time slots against total slots and get the available time slots.

I am trying through this loops but its not working.

for($i=0;$i<count($slot)-1;$i++)                    
{   
    if(count($data)==0)
    {
        $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));        
    }
    else
    {
          for($j=0;$j<count($data);$j++)
          {     
            if(strtotime($slot[$i]) >= strtotime($data[$j]['StartTime']) && strtotime($slot[$i]) <= strtotime($data[$j]['EndTime']))
            {
            //echo "busy slot so it should not be in result.";
            }
            else
            {
             $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));       
            }                       
         }
     }
 }

But its giving the result twice. and not filtering the busy records properly.

Can anyone please help me to find out my mistake and get the correct available time slots ?

4

1 回答 1

0
for($i=0;$i<count($slot)-1;$i++)
{   
    if(count($data)==0)
    {
        $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));        
    }
    else
    {
        $free = true;
        for($j=0;$j<count($data);$j++)
        {     
            if(strtotime($slot[$i]) >= strtotime($data[$j]['StartTime']) && strtotime($slot[$i]) <= strtotime($data[$j]['EndTime']))
            {
                  $free = false;
                  break; // Not necessary to check the rest
            }

        }
        if ($free) {
            $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));
        }
    }
}
于 2013-05-16T00:16:23.270 回答