Problems when generating schedules dynamically

Asked

Viewed 77 times

0

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

For example: From 8:00 am to 3:30 pm, with a 15-minute break 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 the final value, the code does not generate me the hours with minutes, only the full hours. I need it to generate minutes even when it is a full hour set to Final Value, for example 3:00 pm.

NOTE: The script needs to be with is similar to the example, any other solution that follows a different structure can not use in my final code.

  • In fact it generates from 15 to 15 only until the placed... if for 30 only until 30... if for 15 only until 15... if by 45 goes until 45... This is correct ?

  • So the logic is this. If I put 15 it has to generate - 8:00 - 8:15 - 8:30 and so on. The problem is that if I put a final time 15:00 instead of 15:30 as it is in the example, it does not generate me the times with minutes. Only hours 08:00 - 09:00 and so on until the final hour.

  • Always have to be :15, :30, :45 ?

  • That one reply resolve? in case your interval will be PT15M

  • 1

    @RBZ Yes always have to have, if I set a final time without the minutes, in case a full time 15:00 or 14:00 the script does not work properly.

3 answers

1

Do so

I converted first to minutes, to facilitate the process.

$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);
}
  • Why the .0 in $minutes = ($time[0] * 60 + $time[1] * 1); if it also works without ?

  • Dear friend your solution even worked, but I need the structure to be with an equal is I did in the example because I need to use this generation of timetables integrated to other services that are also dynamic and thus proposed I can not make use of the way I need.

  • Can only by for ?

  • @RBZ Unfortunately yes, it needs to be done as simply as possible, just as I came close to a result with the sample script. If it’s not something like that some other time-related features stop working.

  • I posted in response.

  • @Joãovictor needs them both for for your solution to work?

  • @Ricardorosa The solution proposed by RBZ already met me, with a for was only already solved. The point is that the code needed to be the simplest because I use this generation with other dynamic functionalities.

Show 2 more comments

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>";    
}
  • Thank you @RBZ your solution worked. With it I can generate the schedules without causing problems in the other linked functions.

0

You can return an array with the range values. And then create a function that generates 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>';

Browser other questions tagged

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