Array listing with start and end

Asked

Viewed 130 times

1

Example..

$inicio = '11:00';
$fim = '13:00';
$array = ['10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30'];

I need to list the array.. but using the start value and the end value.. excluding what is before the $inicio.. and whatever is after the $final.

then be alone.

$array = ['11:00', '11:30', '12:00', '12:30', '13:00'];

3 answers

3


Validate the 2 values and populate an array.

$inicio = '12:00';
$fim = '13:00';
$array = ['10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30'];
$times = [];
foreach($array as $time){
    if( ($time >= $inicio && $time <=$fim ) ){
        array_push($times, $time);
    }
}
var_dump($times);
  • 1

    It would be nice to use $times[] = $time instead of array_push, because it makes the code more performatic.

  • vlw @Wallacemaxters, I will use yes.

  • Mano @Luizpillon.. perfect.. but needed to be $times = array();

  • but probably because I’m using Laravel.. I just didn’t use array_push.. I set it directly.. as Wallacemaxters quoted!. anyway vlw mano thanks.. I’m doing so much stuff I don’t know why I couldn’t think of it.

  • wonder! probably Voce is running php < 5.4

1

You can use the function array_filter to make the calculation. In PHP, even if it is string it is possible to make comparisons:

$horas = array_filter($array, function ($hora) use ($inicio, $fim) {
    return $hora >= $inicio && $hora <= $fim;
});

This function will create a new array based on the result of the boolean expression returned by the function. If true, the item is kept in the array. If false, it is ignored.

It is important to note that keys are kept when you use array_filter. In case you need to reorder the array, you can call array_values to solve the problem:

$horas = array_filter($array, function ($hora) use ($inicio, $fim) {
    return $hora >= $inicio && $hora <= $fim;
});

$horas = array_values($horas);
  • 1

    It is important to point out that keys are kept and therefore one should take great care when accessing the values. In the example given in the question, the indexes would be 2 to 6, generating error when trying to access $horas[0], for example.

  • 1

    @Andersoncarloswoss ready :p

  • 1

    Dude really top your tip bro.. Thanks bro

1

For knowledge only. If you are working with the full date and only the hours are important. You can solve the problem easily using the Datetime.

$inicio = new DateTime('2017-08-07 11:00:00');
$fim = new DateTime('2017-08-07 13:00:00');

$array = [
    '2017-08-07 10:00:00',
    '2017-08-07 10:30:00',
    '2017-08-07 11:00:00',
    '2017-08-07 11:30:00',
    '2017-08-07 12:00:00',
    '2017-08-07 12:30:00',
    '2017-08-07 13:00:00',
    '2017-08-07 13:30:00'
];


$horas = [];
foreach($array as $hora) {
    $hora = new DateTime($hora);
    if ($hora >= $inicio && $hora <= $fim) {
       array_push($horas, $hora->format('H:i'));
    }
}

var_dump($horas);

//Saída
//array(5) {
//    [0]=>
//  string(5) "11:00"
//    [1]=>
//  string(5) "11:30"
//    [2]=>
//  string(5) "12:00"
//    [3]=>
//  string(5) "12:30"
//    [4]=>
//  string(5) "13:00"
//}
  • 1

    vlw bro by the attention.. I wasn’t using no.. it’s just the date same.. but vlw by the tip.. who knows another day I need.. haha! vlw brother thanks!

Browser other questions tagged

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