Problems generating dynamically schedules

0

I am using the following script to generate hours dynamically between a Start Value and a Final Value with a time interval.

For example: From 8:00 am to 3:30 p.m., with a 15 minute interval between times.

Code:

<?php
$start = '08:00';
$end = '15:30';

$st = explode(':', $start);
$ed = explode(':', $end);

for($hour = $st[0]; $hour <= $ed[0]; $hour++)
{
    for($min = $st[1]; $min < 60; $min += 15)
    {
        if($ed[1] >= $min and $hour <= $ed[0])
            echo "Hour: {$hour}:{$min}<br/>";
    }
}

But if I put 15:00 as final value, the code does not generate the hours with minutes, only the complete hours. I need it to generate the minutes even when it is a full hour set to Final Value, such as 15:00.

NOTE: The script must be com for similar to the example, any other solution that follows a different structure I can not use in my final code.

    
asked by anonymous 05.04.2018 / 19:02

3 answers

1
// Variáveis recebidas
$inicio = '08:15';
$fim = '12:30';

// Quebra horas de minutos
$t1 = explode(':', $inicio);
$t2 = explode(':', $fim);

// Converte para minutos
$min_i = ($t1[0] * 60 + $t1[1]);
$min_f = ($t2[0] * 60 + $t2[1]);

// Define o formato
$formato = '%02d:%02d';

// Loop para impressão
for ($i = $min_i; $i <= $min_f; $i += 15) {

    // Transforma de minutos para horas
    $horas = floor($i / 60);
    // Divide os minutos e tras o resto
    $minutos = ($i % 60);

    // Imprime
    echo 'Hora: ' . sprintf($formato, $horas, $minutos);
    echo "<br>";    
}
    
05.04.2018 / 19:41
1

Do it this way

Converted first to minutes, to make the process easier.

$start = self::horasToMinutes('08:00');
    $end = self::horasToMinutes('15:30');
    $dif = $end - $start;
    for($i = $start; $i <= $end; $i += 15){
        echo "Hora:".self::convertToHoursMins($i);
        echo "<br>";
    }


function horasToMinutes($horas){
    $time    = explode(':', $horas);
    $minutes = ($time[0] * 60.0 + $time[1] * 1.0);
    return $minutes;
}

function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}
    
05.04.2018 / 19:30
0

You can return an array with the range values. And then create a function that manages the results. So you can manipulate these values in other functions more easily. Example:

function add_interval_to($start , $end , $interval){

      /**
     * Obtém um array com horários calculados apartir de um intervalo 
     * entre horas sendo acrescidos '$interval' minutos.
     * definidos.  
     * 
     * @param string $start
     * @param string $end
     * @param int $interval  *somente minutos
     * @return array
     * 
     */

    $n = 0;  
    for($s = 0 ; $s == $n ; $s++){
        $time    = '+ ' . $interval*$n . ' minute';            
        $times[$s]  = date('H:i:s', strtotime( $time  , strtotime($start)));

        if($times[$s] > $end){
            array_pop($times);
            break;
        }

        $n++;
    }

    return $times;
}

function print_intervals($intervals){
    foreach($intervals as $value){
        echo 'Hora: ' . $value . '</br>'; 
    }
}

echo '<pre>';
var_dump(add_interval_to('08:00:00', '15:30:00'  , 15));
echo '</pre>';

//OU

echo '<pre>';
print_intervals(add_interval_to('08:00:00', '15:30:00'  , 15));
echo '</pre>';
    
05.04.2018 / 20:29