Loop validating days of the week in PHP

Asked

Viewed 323 times

1

I have the following period:

15/10/2017 a 14/11/2017 

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

I need, through PHP, to build a loop for me to list all available dates based on that period, and also based on the days of the week that were 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 make it easier, I worked out this way:

<?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>

inserir a descrição da imagem aqui

1 answer

2

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

For this you can build an array with the days of the week in text starting 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 saves the corresponding date by moving forward day by day based on the class DateTime and in the duties add, 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
}

Then just show the labels <input> 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’s day of 15-10-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 sent, it only takes the dates you want, being simple to handle in PHP.

Example working on Ideone

Browser other questions tagged

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