List days of the week, having the month and year PHP

0

I want to print the days of the week separated by the number of the week of a given month: What I have so far is:

function days_week($date = NULL){

$date = ($date == NULL) ? date('m/d/Y') : $date;
$ts = strtotime($date);
$year = date('o', $ts);
$week = date('W', $ts);

$return = [];
for($i = 0; $i <= 6; ++$i) {
    $ts = strtotime($year.'W'.$week.$i);

    $week_month = date('w', strtotime($ts));
    $return[$week_month][$i]['day_week'] = date("d/m/Y", $ts);
    //echo $return['day_week'];
    switch(date("l", $ts)){
        case 'Sunday':
            $return[$week_month][$i]['day_name'] = "domingo";
        break;
        case 'Monday':
            $return[$week_month][$i]['day_name'] = "segunda";
        break;
        case 'Tuesday':
            $return[$week_month][$i]['day_name'] = "terça";
        break;
        case 'Wednesday':
            $return[$week_month][$i]['day_name'] = "quarta";
        break;
        case 'Thursday':
            $return[$week_month][$i]['day_name'] = "quinta";
        break;
        case 'Friday':
            $return[$week_month][$i]['day_name'] = "sexta";
        break;
        case 'Saturday':
            $return[$week_month][$i]['day_name'] = "sábado";
        break;
    };
    //echo " - ".$return['day_name'];
    switch(date("M", $ts)){
        case 'Jan':
            $return[$week_month][$i]['month_name'] = 'janeiro';
        break;
        case 'Feb':
            $return[$week_month][$i]['month_name'] = 'fevereiro';
        break;
        case 'Mar':
            $return[$week_month][$i]['month_name'] = 'março';
        break;
        case 'Apr':
            $return[$week_month][$i]['month_name'] = 'abril';
        break;
        case 'May':
            $return[$week_month][$i]['month_name'] = 'maio';
        break;
        case 'Jun':
            $return[$week_month][$i]['month_name'] = 'junho';
        break;
        case 'Jul':
            $return[$week_month][$i]['month_name'] = 'julho';
        break;
        case 'Aug':
            $return[$week_month][$i]['month_name'] = 'agosto';
        break;
        case 'Sep':
            $return[$week_month][$i]['month_name'] = 'setembro';
        break;
        case 'Oct':
            $return[$week_month][$i]['month_name'] = 'outubro';
        break;
        case 'Nov':
            $return[$week_month][$i]['month_name'] = 'novembro';
        break;
        case 'Dec':
            $return[$week_month][$i]['month_name'] = 'dezembro';
        break;
    }
}
    return json_encode($return);
}

Using:

echo "<pre>";
print_r(days_week("04/01/2018"));
echo "</pre>";

Returns the following:

{
"4": [
      {
        "day_week": "25/03/2018",
        "day_name": "domingo",
        "month_name": "março"
      },
      {
         "day_week": "26/03/2018",
         "day_name": "segunda",
         "month_name": "março"
      },
      {
         "day_week": "27/03/2018",
         "day_name": "terça",
         "month_name": "março"
      },
      {
         "day_week": "28/03/2018",
         "day_name": "quarta",
         "month_name": "março"
      },
      {
         "day_week": "29/03/2018",
         "day_name": "quinta",
         "month_name": "março"
      },
      {
         "day_week": "30/03/2018",
         "day_name": "sexta",
         "month_name": "março"
      },
      {
         "day_week": "31/03/2018",
         "day_name": "sábado",
         "month_name": "março"
      }
    ]
  }

But you can see that the result is wrong, because it shows the result until 31/03, when it was to show from 01/04 onwards ... strange that with other days it works normal ... someone Do you have any tips? Thanks!

Goal I want is a JSON of this format:

USER ENTRY

MONTH: 04 YEAR: 2018

OUTPUT (JSON):

{
"1": [
      {
        "day_week": "01/04/2018",
        "day_name": "domingo",
        "month_name": "abril"
      },
      {
         "day_week": "02/04/2018",
         "day_name": "segunda",
         "month_name": "abril"
      },
      {
         "day_week": "03/04/2018",
         "day_name": "terça",
         "month_name": "abril"
      },
      ...
      ...
      {
         "day_week": "07/04/2018",
         "day_name": "sabado",
         "month_name": "abril"
      }
     ],
    "2": [
          {
             "day_week": "08/04/2018",
             "day_name": "domingo",
             "month_name": "abril"
          },
          {
             "day_week": "09/04/2018",
             "day_name": "segunda",
             "month_name": "abril"
          },
           ...................

I apologize to friends because it really was not "understandable" =) I believe you have now given to understand, the 1, 2 ... is the number of the week in the month. And I need the list of days, as described in JSON

    
asked by anonymous 26.04.2018 / 05:55

1 answer

2

ERRORS IDENTIFIED IN YOUR CODE

  • As shown by you in the desired result, with the input of the day 01/04/2018 you would like the dates to be displayed of a total of 5 weeks (in the month of April). However in your code you have a loop of in maximum 7 days, ie only 1 week in the result. So I changed its for by while(true) . This 'while' is broken as soon as the month ends.
  • Another error is the formatting of timestamp . When I said that I did not identify the error, in fact I did not really understand Why does it happen. But I know it exists and I'll show it to you. follow.
  • First you transform the date into timestamp :

    $date = "04/01/2018";
    $ts = strtotime($date);
    

    The returned value is 1522533600 Then you redeem the year and the year in the year:

    $year = date('o', $ts); // isso aqui retorna o ano. OK! (2018)
    $week = date('W', $ts); // isso retorna o numero da semana no ano. OK! (13)
    

    Within the loop you try to use the following code:

    $ts = strtotime($year.'W'.$week.$i);
    

    That returns the value of 1521932400 . That is different from the value of the first day 1522533600 . If you convert this value:

    // $i = 0
    echo date("m/d/Y", strtotime($year.'W'.$week.$i));
    

    You will have the date 25/03/2018 . Exactly the wrong value of json .

    I have created an alternative to arrive at the result. See:

    $dia = "01";
    $mes = "04";
    $ano = "2018";
    $data = $dia."-".$mes."-".$ano;
    
    $dia_da_semana_array = array('Domingo', 'Segunda', 'Terca', 'Quarta', 'Quinta', 'Sexta', 'Sabado'); // lista
    $meses_array = array('', 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'); // lista
    $dia_da_semana_inicial = date('w', strtotime($data)); // pega o dia da semana em inteiro
    $dia_da_semana_inicial_string = $dia_da_semana_array[$dia_da_semana_inicial];  // pega o dia da semana  em string
    
    $arraySemanas = array(); // lista das semanas
    
    $x = (int)$dia_da_semana_inicial;
    $y = 0;
    
    $semana = 1;
    while(true){
    
        // insere no array
        $indexMes = (int)$mes;
        $arraySemanas[$semana][$y]['day_week'] = $data; 
        $arraySemanas[$semana][$y]['day_name'] = $dia_da_semana_array[$x];
        $arraySemanas[$semana][$y]['month_name'] = $meses_array[$indexMes];
    
        // verifica se mudou o mês
        $data = date('d-m-Y', strtotime("+1 day", strtotime($data)));
        $dataVerifi = explode("-", $data);
        if($dataVerifi[1] != $mes){
            // se mudou o mes para o loop
            break;
        }
    
        if($x == 6){
            $x = 0;
            $y = 0;
            $semana++;
        } else {
            $x++;
            $y++;
        }
    
    }
    
    print_r(json_encode($arraySemanas));
    

    See working at ideone

    Your function would look like this:

    function days_week($date = NULL){
    
        $date = ($date == NULL) ? date('d/m/Y') : $date;
    
        $date = explode("/", $date);
        $dia = $date[0];
        $mes = $date[1];
        $ano = $date[2];
        $data = $dia."-".$mes."-".$ano;
    
    
        $dia_da_semana_array = array('Domingo', 'Segunda', 'Terca', 'Quarta', 'Quinta', 'Sexta', 'Sabado'); // lista
        $meses_array = array('', 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'); // lista
        $dia_da_semana_inicial = date('w', strtotime($data)); // pega o dia da semana em inteiro
        $dia_da_semana_inicial_string = $dia_da_semana_array[$dia_da_semana_inicial];  // pega o dia da semana  em string
    
        $arraySemanas = array(); // lista das semanas
    
        $x = $dia_da_semana_inicial;
        $y = 0;
    
        $semana = 1;
        while(true){
    
            // insere no array
            $indexMes = (int)$mes;
            $arraySemanas[$semana][$y]['day_week'] = str_replace("-","/",$data); 
            $arraySemanas[$semana][$y]['day_name'] = $dia_da_semana_array[$x];
            $arraySemanas[$semana][$y]['month_name'] = $meses_array[$indexMes];
    
            // verifica se mudou o mês
            $data = date('d-m-Y', strtotime("+1 day", strtotime($data)));
            $dataVerifi = explode("-", $data);
            if($dataVerifi[1] != $mes){
                // se mudou o mes para o loop
                break;
            }
    
            if($x == 6){
                $x = 0;
                $y = 0;
                $semana++;
            } else {
                $x++;
                $y++;
            }
    
        }
    
        return json_encode($arraySemanas);
    }
    
    echo days_week("01/04/2018");
    

    I hope this helps you.

        
    28.04.2018 / 19:33