Mark day of each week by repeating a number of times

0

I'm making a calendar and I need to create a form that repeats a number of times every weekday.

Example:

Today 09/11/2018

I want you to repeat 3 times the day of the week getting like this?

09/11/2018

16/11/2018

23/11/2018

My code is like this, but I'm not getting it, I used it as a repetition structure. How could I do that?

for ($contador = 0; $contador < 3; $contador++) {

    $data2 = str_replace("-", "/", date('Y-m-d'));

$data= date('d/m/Y', strtotime($data2));     

echo $data."<br>";  

$data = DateTime::createFromFormat('d/m/Y', $data);
$data->add(new DateInterval('P7D')); // 7dias
echo $data->format('d/m/Y');

}
    
asked by anonymous 09.11.2018 / 12:31

1 answer

3

The algorithm is simple, you need:

  • Create a start date
  • Print date
  • Adds 1 week
  • Return to step 2 until the reps are enough
  • Example:

    <?php
    
    $hoje = new DateTime();
    $semana = new DateInterval('P7D');
    $repeticoes = 3;
    
    do {
        echo $hoje->format('d/m/Y') . "\n";  // Printa data
        $hoje = $hoje->add($semana);         // Adiciona uma semana
    } while (--$repeticoes);
    

    Repl.it with working code

        
    09.11.2018 / 12:46