Random Date with PHP exception

Asked

Viewed 295 times

1

I have the following case:

I have opening hours from 08:00 to 12:00 and 13:00 to 18:00.

$hora_manha_min="08:00"; // Pode variar conforme consulta em Web Service (String)
$hora_manha_max="12:00"; // Pode variar conforme consulta em Web Service (String)
$hora_tarde_min="13:00"; // Pode variar conforme consulta em Web Service (String)
$hora_tarde_max="18:00"; // Pode variar conforme consulta em Web Service (String)

However the schedules from 09:00 to 09:15 and 10:45 to 11:10 are already busy

$ocupado_1_ini="09:00"; // Pode variar conforme consulta em Web Service (String)
$ocupado_1_fim="09:15"; // Pode variar conforme consulta em Web Service (String)
$ocupado_2_ini="10:45"; // Pode variar conforme consulta em Web Service (String)
$ocupado_2_fim="11:10"; // Pode variar conforme consulta em Web Service (String)

I need a random time within the office hours that is not within the busy hours and that I have an interval set in variable, example:

$intervalo="00:15"; // Pode variar conforme consulta em Web Service (String)
// Resultados possíveis: 08:30,08:45,09:15,09:30,09:45,10:00,10:15,10:30,11:15,etc

From now on I appreciate the help.

  • Okay, what have you tried to do? Have you managed to generate this random time? Is it just one or do you need the start time and the end time? Busy times are stored in variables as shown in the question?

  • @Andersoncarloswoss I didn’t try to do it because I don’t even know where to start. I only need one time, for example: 15:00. I will change the question, but I need to set a scheduling period, example: 15 in 15 min. Yes everything is in variable as shown.

  • Does it have to be necessarily random? You could not, for example, search for the first available period?

  • @Andersoncarloswoss Unfortunately I need it to be random.

  • I have this solution (https://ideone.com/hFax0f). It works, but I believe it has better ways of doing it. I’ll post an answer soon, but see if you can understand the idea.

  • @Andersoncarloswoss Show That’s just what I needed! Thank you so much then post your reply.

  • In the case, there I considered that the busy hours have equal duration to the interval, but I saw that in his example, $ocupado_2 lasts longer than 15 minutes. Can the duration be even longer than the period defined by the interval? Otherwise, how are you receiving this data? Keeping busy hours in variables can make the solution impossible, because there is no way to know how many times will be. Cannot store in an array?

  • @Andersoncarloswoss the Gap will always be unique, so solve my problem. I put it differently because I thought I was going to use it, but on second thought I don’t have to.

  • Ok, this makes things easier. And how much to store busy schedules in array?

  • If it’s 15:15, shouldn’t the last hour before lunch be 11:00? since it started at 10:45

Show 5 more comments

1 answer

0


function procurahorario($manha_min,$manha_max,$tarde_min,$tarde_max,$intervalo,$horarioocupado){
    // Define o intervalo da manhã:
    $hora_manha_min=new DateTime($manha_min);
    $hora_manha_max=new DateTime($manha_max);
    $hora_tarde_min=new DateTime($tarde_min);
    $hora_tarde_max=new DateTime($tarde_max);
    $intervalo=new DateInterval($intervalo);
    // Lista de todos os horários:
    $horarios=array(new DateTime('00:00'));
    // Adiciona na lista os horários da manhã com intervalo de 15 minutos:
    $temp = clone $hora_manha_min;
    while ($temp <= $hora_manha_max){
        array_push($horarios,$temp);
        $temp = clone $temp;
        $temp->add($intervalo);
    }
    // Adiciona na lista os horários da tarde com intervalo de 15 minutos:
    $temp = clone $hora_tarde_min;
    while ($temp <= $hora_tarde_max){
        array_push($horarios,$temp);
        $temp = clone $temp;
        $temp->add($intervalo);
    }

    $ocupado=explode("/",$horarioocupado);
    $total_ocupado=count($ocupado);
    for($i=0;$i<$total_ocupado;$i++){
        $ini_ocupado=explode("|",$ocupado[$i]);
        // Horários ocupados:
        $ocupado_ini = new DateTime($ini_ocupado[0]);
        // Remove da lista os horários ocupados:
        if(($key = array_search($ocupado_ini,$horarios)) !== false) {
            //echo "Removendo ", $horarios[$key]->format("H:i"), PHP_EOL;
            unset($horarios[$key]);
        }
    }
    // Devolve um horário aleatório:
    $result=$horarios[array_rand($horarios)]->format("H:i");
    return $result;
}

Browser other questions tagged

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