Loop validating days of the week in PHP

1

I have the following period:

15/10/2017 a 14/11/2017 

(with option selected from Monday, Tuesday, Wednesday, Thursday and Friday - Saturday and Sunday are inactive)

I need to loop through PHP to list all available dates based on that period, and based on the days of the week that have been selected.

In this case, the result would be:

16/10/2017
17/10/2017
18/10/2017
19/10/2017
20/10/2017
21/22 são sábado e domingo, por isso não listariam
23/10/2017
24/10/2017
25/10/2017

How could I do this?

To facilitate, I elaborated as follows:

<?php
    header('Content-type: text/html; charset=UTF-8');
    if(isset($_POST['inicio']) && isset($_POST['fim'])){        
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
    }
?>

<form enctype="multipart/form-data" method="post">
    <input type="date" id="inicio" name="inicio"><br>
    <input type="date" id="fim" name="fim"><br><br>

    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="segunda">Segunda<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="terca">Terça<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="quarta">Quarta<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="quinta">Quinta<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="sexta">Sexta<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="sabado">Sábado<br>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="domingo">Domingo<br><br>
    <input type="submit" value="Montar Rotina">
</form>

    
asked by anonymous 15.10.2017 / 20:08

1 answer

2

I suggest building the checkboxes already with the date values that you want. This makes it much easier to interpret from the PHP side.

For this you can build an array with the days of the week in text that begin on Sunday to match the day of the week in PHP:

$diasTexto = Array("Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado");

Then to another array, save the corresponding date by walking day by day on the basis of the class DateTime and add functions, format :

$data = new DateTime(); //construir com a data corrente

for ($i = 0; $i < 7; ++$i){
    $diaSemana = intval($data->format("w")); //obter o dia da semana
    $dias[$diaSemana] = $data->format("d-m-Y"); //guardar no array a data correspondente
    $data->add(new DateInterval('P1D')); //avançar 1 dia
}

Next, just show the <input> tags based on these two arrays:

for ($i = 0; $i < 7; ++$i){
    ?>
    <input type="checkbox" id="diasemana[]" name="diasemana[]" value="<?=$dias[$i]?>"><?=$diasTexto[$i]?><br>
    <?php
}

With this the generated html will have the following aspect (considering today 10-15-2017):

<input type="checkbox" id="diasemana[]" name="diasemana[]" value="15-10-2017">Domingo<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="16-10-2017">Segunda<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="17-10-2017">Terça<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="18-10-2017">Quarta<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="19-10-2017">Quinta<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="20-10-2017">Sexta<br>
<input type="checkbox" id="diasemana[]" name="diasemana[]" value="21-10-2017">Sábado<br>

Now when the form is submitted, it only takes the dates you want, being simple to handle in PHP.

Example working on Ideone

    
15.10.2017 / 20:42