Combobox with 5 minute interval with PHP

Asked

Viewed 496 times

2

Colleagues.

I have a combobox where I plan to store schedules from 07:00 to 22:00 with a 5 minute break. See below:

inserir a descrição da imagem aqui

How do I do this in PHP? Unfortunately I don’t have any code ready in PHP, because I couldn’t find a logic.

<div class="input-group" style="width:100px">
  <select class="form-control" style="width:100px">
          <option value="">07:00</option>
  </select>
  <span class="input-group-addon" style="background-color: #FAFAFA">às</span>
  <select class="form-control" style="width:100px">
    <option value="">07:05</option>
  </select>
  <span class="input-group-btn">
        <button type="submit" name="Buscar" value="PorAcesso" class="btn btn-primary">+</button>
  </span>
</div>

2 answers

2


$hora = '07:00:00';
echo "<select class='form-control' style='width:100px'>";
echo "<option value=''>$hora</option>";
for($i = 0; $i < 180; $i++){
    $hora = date('H:i:s', strtotime('+5 minute', strtotime($hora)));
    echo "<option value=''>$hora</option>";
}
echo "</select>";

0

PHP natively already has a class called Datetime, and with it it is possible to perform several manipulations on the created date object.

With the help of another resource called Dateinterval, it is possible to make these modifications.

To solve this problem, you can create a repeat node that adds 5 minutes to each iteration.

See an example below:

$hora = DateTime::createFromFormat('G:i', '07:00');

while ($hora->format('G') <= 22) {
    $hora->add(new DateInterval('PT5M'));
    echo $hora->format('G:I');
}

Note: The above code is just an example, and will not take into account minutes if the time is still within 22h.

Browser other questions tagged

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