Search in DB with For Repetition

1

I have the following table as an example:

data - chave primaria, contendo a data mesmo 2018-01-02.
diasemana - int, contendo o dias das semana tipo 1 para segunda, 2 para terça.
semanaAno - int, contendo o numero de semana do ano tipo 1 primeira semana do ano.

So I have:

<?php
$repetiacao = 3;
 for ($i = 0; $i <= $repetiacao ; $i++) {
    // aqui está o problema
 }
?>

"Here's the problem": I wanted every loop to repeat the date, for example 2018-01-02 on the first loop after 2018-02-02 on the second and last 2018-03-02 on according to the amount of repetition

As you can see, it raises the MONTH for each repetition but still my echo I need it to be the semanaAno field of my table. So I came to the logic more or less that in each loop I make a select in the table showing semanaAno when data = data . Can you help me?

    
asked by anonymous 01.01.2018 / 23:45

1 answer

1

From the date you can count the number of weeks. For this you can use the DateTime class and the format () . It's also interesting to know the values used to format .

Your code looks like this:

for($i = 1; $i <= 3; $i++){
   $data = new Datetime('2018-' . $i . '-02');

   echo '<br><br>';
   echo 'data: ' . $data->format('Y-m-d');
   echo '<br>Semana do ano: ' . $data->format('W');
   echo '<br><br>';
}

This will print:

data: 2018-01-02
Semana do ano: 01



data: 2018-02-02
Semana do ano: 05



data: 2018-03-02
Semana do ano: 09

Where W returns the number of weeks. You can check other formatting options here at php.net .

One more detail, if you go to past documentation, you'll see that the first day of the week is Monday for the W option.

    
02.01.2018 / 01:13