(PHP MYSQL) Split number of hours in number of users and save in mysql

-1

Good morning, fellow programmers. I would like help, I have a mysql database with a certain amount of users, and I need to get the range of hours (ex: 10:00 p.m. to 2:00 p.m.) and divide by the number of users, and then set the value divided for each user. That is, suppose I have 4 users, divide the time interval and give one hour for each. So I need this division result to be entered for the users.

Ex: It divides the interval (10:00 to 14:00), and assigns to each user.

user1 = 10:00 a.m. to 10:59:59

user2 = 11:00 a.m. to 11:59:59

user3 = 12:00 at 12:59:59

user4 = 1:00 p.m. to 2:00 p.m.

    
asked by anonymous 15.03.2018 / 15:53

1 answer

0

Come on, to divide you need the starting and ending time, it is always one bigger than the other.

   <?php

     $hora_inicial = strtotime("10:00:00");
     $hora_final = strtotime("14:00:00");

     $intervalo = abs($string2 - $string1);
     //Agora que temos o intervalo vamos verificar quantos usuários existem no banco

     $mysqli = new mysqli("host","user","password","db");

     $count = 0;
     if($sth = $mysqli->query("SELECT * FROM suatabela")){
         while ($row = $sth->fetch_assoc()) {
            $count++;
         }
     }

     //Agora o count tem a quantidade de usuários, então pegamos o intervalo e dividimos pela quantidade.
     $intervalo = ($intervalo/$count);

    //Agora mandamos para o banco:
    //Utilziae insert ou update, depende da sua lógica aí
    if($mysqli->query("SELECT * FROM suatbela"){
          while ($row = $sth->fetch_assoc()) {
          //pego o id do usuário atual.
          $id = $row['id'];

          //Converter os segundos em horas
          $hora_inicial2 = gmdate("H:i:s", $hora_inicial);

          //Hora final é a hora inicial + o intervalo
          $hora_final2 = gmdate("H:i:s", $hora_inicial+$intervalo);

          //Então você seta a nova hora inicial para a ultima final para que no próximo loop a seja uma nova hora inicial
          $hora_inicial = strtotime("$horafinal2");

          //E faço o update em seu id no campo do intervalo
          $mysqli->query("UPDATE suatabela SET intervalo = '$hora_inicial2 às $hora_final2' WHERE id='$id'");
          }
    }
    
15.03.2018 / 16:45