Remove array schedules if you do not meet a certain number of required periods

Asked

Viewed 82 times

0

I have an array with available schedules, being these schedules in a 30 minute interval:

$arrHoras = ["08:30", "09:00", "09:30", "10:00", "10:30", ... , "18:30", "19:00"];

After some filters that are reserved schedules, I have the following array:

$arrHoras = array(
    2 => "09:00",
    3 => "09:30",
    6 => "11:00",
    7 => "11:30",
    10 => "13:00",
    11 => "13:30",
    12 => "14:00",
    13 => "14:30",
    16 => "16:00",
    17 => "16:30",
    21 => "18:30"
);

What I need to do is make one more filter by removing times that do not satisfy a number of consecutive periods required. All of them meet the 1 that is himself, but if it is two I need it and one more time in a row, and can not jump, for example, from 9 to 9:30 ok, but from 9:30 to 11 no, because jumps the sequence.

If I need 2 schedules then the return would be this:

$arrHoras = array(
    2 => "09:00",
    6 => "11:00",
    10 => "13:00",
    11 => "13:30",
    12 => "14:00",
    16 => "16:00"
);

But if I need 3 times then the return would be:

$arrHoras = array(
    10 => "13:00",
    11 => "13:30"
);

And if I need 4 or more times will return me an empty array.

  • A question, if that array were ordered you would want that $qtdeSlots is the interval between the keys of the array's?

  • Example: in a array ordered where the $qtdslots for 2 the order of the keys was to be 0,2,4,6,8..., if $qtdslots for 3 0,3,6,9,12... and so on. That’s it?

  • So, not quite this, every slot of mine is 30 minutes. I can see even by the keys the count because initially I create this Arras dynamically and have all keys in order but with the filters will removing the hours that are not necessary, I only have to remove by the slots Qtde.

  • Fine, but if each slot you own is 30 minutes, the key element 11 of array should in the answer, because he does not obey this rule.

  • What happens if $qtdeSlots is equal to 2, for example, I need 2 slots of 30 minutes in a row to be able to accept. I have the 9:00 and 9:30, so I have 2 slots in a row, so the 9:00 is accepted, then I have the 9:30 and the 11:00 are not two in a row, so the 9:30 is not accepted, and so I do until the end. I hope I’ve been a little clearer

  • You’re still confused. Can you edit the question by zooming in on the context, all of a sudden even posting more code from how you get to that filtered array? Suddenly there’s a simpler alternative...

  • You need all intervals where the $qtdeSlots is equal to 2 (or number q vc put) or needs it to be greater than or equal to 2?

Show 2 more comments

2 answers

1


Solution I’ve achieved so far:

$arrHoras = array(
        2 => "09:00",
        3 => "09:30",
        6 => "11:00",
        7 => "11:30",
        10 => "13:00",
        11 => "13:30",
        12 => "14:00",
        13 => "14:30",
        16 => "16:00",
        17 => "16:30",
        21 => "18:30"
    );

$qtdeSlots = 2; // isso é variável 

$arrHorasOk = array();

for ($i=0, $c = count($arrHoras); $i < $c; $i++) {      
    $current = current($arrHoras);

    for($j = 1; $j < $qtdeSlots; $j++) $next = next($arrHoras);

    $slots = count(dateRange($current, $next, '30 minutes', 'H:i' ));
    if($slots == $qtdeSlots)
        $arrHorasOk[] = $current;

    for($k = 1; $k < $qtdeSlots-1; $k++) prev($arrHoras);

}

echo "<pre>";
var_dump($arrHorasOk);
echo "<pre>";



function dateRange($first, $last, $step = '+1 day', $format = 'Y-m-d H:i:s' )
{
    $dates   = array();
    $current = strtotime($first);
    $last    = strtotime($last);

    while( $current <= $last ) {    
        $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }
    return $dates;
}

0

In order to calculate how many slots exist in the difference between one time and another you can use the time calculations

Calculus

$d1 = new DateTime("0000-00-00 09:00:00", new DateTimeZone('America/Sao_Paulo'));
$d2 = new DateTime("0000-00-00 11:30:00", new DateTimeZone('America/Sao_Paulo'));
echo ($d1->diff($d2)->i + $d1->diff($d2)->h * 60) / 30; // retornará 5 que é o valor referente ao slot atual

From there you can go through your array and implement the rules you need.
Obs: Use with php 5.4 or higher.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.