How to get the days of the current week sorted Sunday / Monday

2

I have a grid of some time notes that I have a filter for user fills the date and loads my grid with the records and I have a <thead> that has the days of the week, I'm getting the right days, but it stays that way :

Segunda 12
Terça   13
Quarta  14
Quinta  15
Sexta   9
Sabado  10
Domingo 11

It's taking days from a previous week, but if I click on Thursday it sorts correctly.

functiondiaSemana($variavel2,$dayName){if($variavel2!=null){$orderdate=explode('/',$variavel2);$day=$orderdate[0];$month=$orderdate[1];$year=$orderdate[2];$mes=date("m");
        $ano = date("YYYY");
        $dia = date("d");
        $dia_semana = date("w");


        $mes = $month;
        $ano = $year;
        $dia = $day;

    }

    $cont=0;

    While($cont<=6)
    {
        $dia_calendario = date("d/m/Y",mktime(0,0,0,$mes,$dia-$dia_semana,$ano));
        $dia_s_calendario = date("w",mktime(0,0,0,$mes,$dia-$dia_semana,$ano));

        if($dayName == $dia_s_calendario ){
            return $dia_calendario;
        }
        $dia_semana--;
        $cont++;
    }
}






<?php if(isset($_POST['variavel'])){?>                  
    <thead>
        <th id='thDomingo' class='trday'>DOMINGO<br><div id ='domingo' class='day' onclick='dataSemana(0)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),0); ?></th>
        <th id='thSegunda' class='trday'>SEGUNDA<br><div id ='segunda' class='day'onclick='dataSemana(1)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),1); ?></div></th>
        <th id='thTerca' class='trday'>TERÇA<br><div id ='terca' class='day' onclick='dataSemana(2)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),2); ?></div></th>
        <th id='thQuarta' class='trday'>QUARTA<br><div id ='quarta' class='day'onclick='dataSemana(3)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),3); ?></div></th>
        <th id='thQuinta' class='trday'>QUINTA<br><div id ='quinta' class='day' onclick='dataSemana(4)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),4); ?></div></th>
        <th id='thSexta' class='trday'>SEXTA<br><div id ='sexta' class='day' onclick='dataSemana(5)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),5); ?></div></th>
        <th id='thSabado' class='trday'>SÁBADO<br><div id ='sabado' class='day' onclick='dataSemana(6)'><?php echo $dias = diaSemana($variavel2 = implode("/",array_reverse(explode("-",$_POST['variavel']))),6); ?></div></th>                             
    </thead>
<?php }  ?>
    
asked by anonymous 02.06.2017 / 14:16

2 answers

3

I did the following, I get the current day, I go back to the date for the first day of the week, and I loop until Sunday.

The following code has been added:

<?php

$translate = array(
    0 => "Dom",
    1 => "Seg",
    2 => "Ter",
    3 => "Qua",
    4 => "Qui",
    5 => "Sex",
    6 => "Sab",
);

$data = new DateTime('2017-12-12');     // Pega a data de hoje
$diaN = date( "w", strtotime($data->format('Y-m-d'))); // Dia da semana, começa em 0 com domingo, 1 para segunda...

$data->modify('-' . $diaN . ' day');

for($i=0;$i<=6;$i++) {
    echo $data->format('d/m/Y') . ' - ' .  $translate[$data->format('w')] . "<br>";
    $data->modify('+1 day');
}

Image of the result of the code above:

    
02.06.2017 / 14:47
0

Well, at first it works, but it happens the same in my code.

Try changing:

$data = new DateTime');

By:

$data = new DateTime('12-12-2017');

It returns me:

07/12/2017 - Thu
08/12/2017 - Sex
09/12/2017 - Sab
10/12/2017 - Sun
11/12/2017 - Mon
12/12/2017 - Tue
12/13/2017 - Wed

If I were to sort this out, it would look like this:

10/12/2017 - Sunday
11/12/2017 - Mon
12/12/2017 - Tue
12/13/2017 - Wed
07/12/2017 - Thu
08/12/2017 - Sex
09/12/2017 - Sat

    
02.06.2017 / 15:59