Locate specific day of the week between dates

Asked

Viewed 123 times

3

I have the following code to release on an agenda days and times available and for that I use the following routine:

    //recebo dados do formulario
    //inclusive o dia da semana ex.: Friday
    $dia_semana = $_POST['dia_semana'];
    $hora = $_POST['hora']; 
    $laboratorio = $_POST['laboratorio'];
    $quantidade = $_POST['quantidade']-1;

    //pega a data de hoje
    //para verificar qual a proxima data correspondente ao
    //dia enviado pelo formulario
    // como por exemplo, proxima sexta(Friday)
    $hoje = date('Y-m-d');
    $dia = date("Y-m-d", strtotime("$hoje next $dia_semana"));

        $dados = array(
            'data' => $dia,
            'hora' => $hora,
            'laboratorio' => $laboratorio
        );
        //cadastra no banco a primeira data usando minha funcao
        $cadastrar = DBCreate('nw_vip',$dados);

      //faz um loop para cadastrar as proximas datas
      //com o dia escolhido limitado a quantidade
      //passada pelo formulario
      for($i=0;$i<$quantidade;$i++)
      {
       //se a primeira sexta caiu no dia 10
       //caucula as proximas somando 7 dias e cadastrando
       $dia =  date("Y-m-d", strtotime("$dia +7 day"));
       $dados = array(
         'data' => $dia,
         'hora' => $hora,
         'laboratorio' => $laboratorio
       );
      //cadastra no banco usando e minha função
      $cadastrar = DBCreate('nw_vip',$dados);
}

My problem if I launch 5 Fridays today and then launch another 5 Fridays, it will repeat the dates.

Then I would need that the second time I was going to release it could consider the last Friday already registered in case there are any and register from it.

Example: I throw 3 Fridays at 8hs:

Sex 24/06 8:00
Sex 01/07 8:00
Sex 08/07 8:00

If later I release again 5 more next day continued from Friday day 15/08. How to make an inquiry to know the date of the last Friday already registered, as my example. Of course it could be another day and even another time.

1 answer

3


You can make a query in your table looking for the last occurrence of the day of the week who is trying to schedule. For the example of Friday it looks like this:

SELECT MAX(data_agenda) AS ultima_data
  FROM minha_agenda
 WHERE WEEKDAY(data_agenda) = 5; 
/* semana iniciando em 0 para domingo, então 5 é sexta-feira, ok? */

You can change the query to the WHERE contemplate more restrictions, such as time, laboratory, or any other information you wish.

The result of the consultation will always be the last date in its context. If it turns out empty, means that you still has no record and can start with the same day informed by the user.

Information about the function WEEKDAY here: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

Observing: it is also possible to use the function DAYOFWEEK, which uses the week pattern started at 1 for Sunday, 2 for Monday and so on.

  • The MAX function in this context returns me the 'highest' date, in the case that is farthest in the future?

  • The function returns you the longest date you find in the database table, for the constraints you set in the query. Including future dates already recorded in the table.

Browser other questions tagged

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