Combobox of 5-minute intervals with PHP

2

Colleagues.

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

HowdoIdothisinPHP?UnfortunatelyIdonothaveanycodereadyinPHP,becauseIdidnotfindalogic.

<divclass="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>
    
asked by anonymous 22.06.2017 / 18:26

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>";
    
22.06.2017 / 18:33
0

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

With the help of another feature called DateInterval , you can make these changes.

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 code above is just an example, and will not take into account the minutes if the time is still within 10 pm

    
22.06.2017 / 18:52