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

Asked

Viewed 1,161 times

2

I have a grid of some hours notes that I have a filter pro user fills the date and loads my grid with the records and I have a <thead> that has the days of the week, I am able to seek the right days, but it stays like this:

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

This picking up days from a previous week, but if I click on Thursday it orders correctly.

inserir a descrição da imagem aqui

function diaSemana($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 }  ?>
  • What is the expected result? The days of the current week?

2 answers

3


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

There was the following code:

<?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');
}

Screenshot of the above code result:

inserir a descrição da imagem aqui

  • Well, it works at first, but it happens the same way in my code. $data = new Datetime'); Por: $data = new Datetime('12-12-2017'); it returns to me: 07/12/2017 - Qui 08/12/2017 - Sex 09/12/2017 - Sab 10/12/2017 - Dom 11/12/2017 - Seg 12/2017 - Ter&##Xa;13/12/2017 Qua If I am going to order this, it would look like this: 10/12/2017 - Dom 11/12/2017 - Seg 12/12/2017 - Ter 13/12/2017 - Qua 07/12/2017 - Qui 08/12/2017 - Sex 09/12/2017 - Sab

  • I made the correction, I was always spending the day of the week as the current date and not the set date. Now it is correct. Before it worked because it was the current date set manually and dynamically picked it too.

  • Colleague, thank you for now, is already giving a lot of strength, but it has not generated my result, I’m passing this date by parameter. EX: $data = new Datetime($dataComboBox); the $dataComboBox variable receives the value of an input(datepicker) that the user selects, which then refreshes the grid with ajax.

  • You need to format the date to the American format. Being "YEAR-MONTH-DAY", in your example below it seems to me that arrived "DAY-MONTH-YEAR". Just reverse the orders, this way $data = implode('-', array_reverse(explodes('-', $data))), this way "12-12-2017" will be "2017-12-12".

  • Here’s what I did. $dataComboBox = date("d/m/Y", strtotime($dataReceived)); $data = new Datetime($dataComboBox); // Picks up today’s date $Dian = date( "w", strtotime($data->format('Y-m-d'))); // Day of the week, starts at 0 with Sunday, 1 for Monday... $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'); } Until the 13th or 2nd week, it brings me the values correctly. After that of the conversion error and brings : 28/12/1969

  • I got it, mate, it was the variable that was coming by parameter that was in the wrong format, Tks.

Show 1 more comment

0

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

Try to trade:

$data = new DateTime');

For:

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

he returns me:

07/12/2017 - Thu
08/12/2017 - Friday
09/12/2017 - Sab
10/12/2017 - Dom
11/12/2017 - Monday
12/12/2017 - Tuesday
13/12/2017 - Wed

If I were to sort it out, I’d be like this:

10/12/2017 - Dom
11/12/2017 - Monday
12/12/2017 - Tuesday
13/12/2017 - Wed
07/12/2017 - Thu
08/12/2017 - Friday
09/12/2017 - Sab

Browser other questions tagged

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