Valid times

Asked

Viewed 95 times

2

I have a array with multiple schedules, may sometimes have only 4 and other N times.

I just need to get some records.

Follows my code:

$array = array(
      0  => "08:00:00",
      1  => "08:02:00",
      2  => "08:04:00",
      3  => "08:08:00",
      4  => "08:10:00",
      5  => "10:59:00",
      6  => "11:00:00",
      7  => "11:05:00",
      8  => "11:11:00",
      9  => "13:00:00",
      10 => "13:03:00",
      11 => "13:06:00",
      12 => "13:08:00",
      13 => "18:00:00",
      14 => "18:03:00",
      15 => "18:06:00",
      16 => "18:35:00",
      17 => "19:15:00"
);

$auxiliar = null;
foreach ($registros as $indice => $registro) {
    if ($auxiliar === null) {
        $auxiliar = $registro;
        continue;
    }

    $registroAtual   = current($registros); //Registro atual
    $proximoRegistro = next($registros);    //Proximo registro

    //Tem um proximo registro
    if($proximoRegistro){
      //Diferença entre dois horários
      $diffHorarios = $this->calculaTempo($registroAtual, $proximoRegistro);

      //Houve registros no intervalo de 10 minutos
      if($diffHorarios <= '00:10:00'){
        unset($registros[$indice]);
      }
    }
  }
  • Your routine should take the first time and add it to a variable and the list of valid times. Take the second and compare your variable plus the margin you want. If it is within that margin you discard. Read the second and do the same thing. It follows.... Reads the very same - this one outside your margin. Updates its variable and adds it to the list of valid schedules. Read the enessima + 1 and check the margin and follow. In the end you will have the list of valid schedules.

  • How would that look in the code ?!

  • anyone ??? help ?

  • @Lucascoelho I made an example, I hope it helps, else the right result in your question has different data so please note the array by 18:06 should not appear ! I believe not ... kkk

  • @Lucascoelho I will improve wait a lot I warn you.

  • @Virgilionovic ok, thank you!

  • @Virgilionovic Any doubt I’m at your disposal, thank you

  • 1

    @Virgilionovic It worked, thank you very much!

Show 3 more comments

1 answer

1


Always when using data type use datetime class and the class Dateinterval:

$array = array(
    0  => "08:00:00", 1  => "08:02:00", 2  => "08:04:00", 3  => "08:08:00",
    4  => "08:10:00", 5  => "10:59:00", 6  => "11:00:00", 7  => "11:05:00",
    8  => "11:11:00", 9  => "13:00:00", 10 => "13:03:00", 11 => "13:06:00",
    12 => "13:08:00", 13 => "18:00:00", 14 => "18:03:00", 15 => "18:06:00",
    16 => "18:35:00", 17 => "19:15:00");

function result_time_array(array $values)
{
    $index = 0;
    $c = date_create_from_format('H:i:s', $values[0]);
    $agr[$index][] = $values[0];
    for ($i = 1; $i < count($values); $i++)
    {
        $d = date_create_from_format('H:i:s', $values[$i]);
        $diff = $c->diff($d);
        if (!($diff->i <= 10 && $diff->h === 0))
        {
            $index++;
        }
        $c = date_create_from_format('H:i:s', $values[$i]);
        $agr[$index][] = $values[$i];
    }
    $st = 'e';
    $index = 0;
    foreach ($agr as $key => $value)
    {
        if ($st === 'e')
        {
            $result[$index][$st] = $value[0];
            $st = 's';
        }
        else if ($st === 's')
        {
            $result[$index][$st] = end($value);
            $st = 'e';
            $index++;
        }
    }
    return $result;
}

var_dump(result_time_array($array));

Exit:

array(3) {
  [0]=>
  array(2) {
    ["e"]=>
    string(8) "08:00:00"
    ["s"]=>
    string(8) "11:11:00"
  }
  [1]=>
  array(2) {
    ["e"]=>
    string(8) "13:00:00"
    ["s"]=>
    string(8) "18:06:00"
  }
  [2]=>
  array(2) {
    ["e"]=>
    string(8) "18:35:00"
    ["s"]=>
    string(8) "19:15:00"
  }
}

PHP Sandbox

  • almost this however the result has to give: 08:00 - 11:11 / 13:00 - 18:06 / 18:35 - 19:15

  • the result has to give what?

  • 08:00 - 11:11 / 13:00 - 18:06 / 18:35 - 19:15

  • @Lucascoelho I made the code edition, sorry I had to give an exit!

Browser other questions tagged

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