(PHP MYSQL)

Asked

Viewed 187 times

-1

Good morning, fellow programmers. I would like an aid, I have a database in mysql with a certain amount of users, and I need to take the interval of hours (ex: 10:00 until 14:00) and divide by the amount of users, and then set the value divided for each user. That is, let’s assume that I have 4 users, then divide the interval of hours and give one hour each. Then I need this division result to be inserted for users.

Ex: Divide the range (10:00 to 14:00), and assign to each user.

user1 = 10:00 to 10:59:59

user2 = 11:00 to 11:59:59

user3 = 12:00 to 12:59:59

user4 = 13:00 to 14:00:00

  • So that we can help, give some concrete example, for example the structure of this table where you store this data.

1 answer

0


Come on, to divide you need the initial and final hour, it’s always being 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'");
          }
    }
  • @Programmer lunatic what I did there was just a simple example, man, so if it helps you apply your logic and conclude, because you did not put anything of your code, can not know what you are doing

  • No Pow, I could not write everything, my son pressed here and I can not edit the answer kkkkk, I was going to send this.

  • Dude, I started doing something similar, but yours is much more elaborate. The idea of what I want to do, is an office system. So, I have the patients and the times they say they can go to the office, like, patient1 can go between 8:00 and 10:00 in the office, it can’t go past that time. But the office is open from 08:00 until 19:00. Then I have to adjust the schedules for each patient, within the office hours but respecting the time that the customer can go. Madness right? kkkkk That’s why I’m breaking my head kkkk

  • Ahhhhh ok, so you have the hours set and when you go casting you do not want to go past the maximum time, right?

  • By your question I understood that you were going to launch a final and initial time and all registered customers took this interval divided by the total and separated the same amount for all

  • Exactly. I have clients in the bank, and each one has a time limit. Type, patient1 can only go from 08:00 to 10:00, after that time, he cannot. And the office hours are 08:00 to 19:00. So, I need to divide the interval from 08:00 to 19:00 by the amount of patients I have, and the divided value, go inserting for each patient, as long as it is within the time that he can go. user1 = 10:00 at 10:59:59 user2 = 11:00 at 11:59:59 user3 = 12:00 at 12:59:59 user4 = 13:00 at 14:00:00

  • @Lunatic programmer ah then my example fits perfectly, in case you have some syntax error I ask forgiveness kkkkkk, I did right here, so I may end up typing something wrong, but as each day you must change the schedules, you must also add the field of the day, if you’re not going to split it up for every customer of all time

  • No man, your example is great, I’m fitting the ideas here. I’ll try to solve the problem, and I’ll tell you.

  • @Lunatic programmer blz kkkk, if you solve your problem there and you want to mark my answer as accepted I thank also kkkkkk

  • It took me a while to respond, but it all worked out nicely. Vlw partner!

Show 5 more comments

Browser other questions tagged

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