Search in BD with For Repetition

Asked

Viewed 28 times

1

I have the following Table as 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 each repeat loop to pick up the date, for example 2018-01-02 on the first loop after 2018-02-02 on the second and last 2018-03-02, according to the repeat amount

As you see it increases the MONTH in each repetition but still mine echo I need you to be the field semanaAno of my table. Then I arrived at logic more or less that in each loop I make a select in the table showing the semanaAno when data = data. Can you help me?

1 answer

1

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

Your code can stay that way:

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 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 on php.net.

One more detail, if you consult the past documentation, you will see that the first day of the week is Monday, for option W.

  • Great, I’ll test here and return, thank you very much

  • Dear Juven_v, yes gave certificate for what I need, in case I want to take also the day of the week of each tie would be like? example 2018-02-02 would be a Friday in case number 5

  • i put the $data->format('N'); as I saw in the documentation. That’s right? That’s right?

  • Yes, that is correct

  • So all right, thank you.

  • But a question and if the Year has dynamic, example I start in December 2018 and I have repeat 3 times, automatically it would have to skip to 2019

  • You could use the function add() class DateTime, available example here. Then the variable $i of the for loop would not need to be concatenated with the date. The date advance would be done with the cited function.

  • I will consult and return thank you

Show 3 more comments

Browser other questions tagged

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