SELECT HTML field displaying Previous, current and next month only

Asked

Viewed 1,233 times

2

I need to make the SELECT field that today is like this (code below) displayed only the previous, current and next month. For example: As we are in the month of MAY so it will display in this select (html) only the months of APRIL MAY and JUNE and as we advance months will display MAY, JUNE and JULY....

I’m getting system ready to modify here in stage what I currently have code is this:

<select class="form-control" style="width: 200px;" name="Mes" id="Mes" onblur="verificaDataBaixaBoleto();">
                                <?php

                                $y = date('n'); // Mês 1 a 12

                                    if($this->mes){
                                        $mesatual = $this->mes;
                                    }
                                    for($x=1;$x<=12;$x++){
                                        $mes = $x;
                                        echo "<option";
                                        if($x == $mesatual){
                                            echo " selected='selected'";
                                        }
                                        echo " value='";
                                        echo $mes;
                                        echo "'>";

                                        if($x == 1){
                                            echo "Janeiro";
                                        }
                                        if($x == 2){
                                            echo "Fevereiro";
                                        }
                                        if($x == 3){
                                            echo "Março";
                                        }
                                        if($x == 4){
                                            echo "Abril";
                                        }
                                        if($x == 5){
                                            echo "Maio";
                                        }
                                        if($x == 6){
                                            echo "Junho";
                                        }
                                        if($x == 7){
                                            echo "Julho";
                                        }
                                        if($x == 8){
                                            echo "Agosto";
                                        }
                                        if($x == 9){
                                            echo "Setembro";
                                        }
                                        if($x == 10){
                                            echo "Outubro";
                                        }
                                        if($x == 11){
                                            echo "Novembro";
                                        }
                                        if($x == 12){
                                            echo "Dezembro";
                                        }

                                        echo "</option>";

                                    }
                                ?>
                        </select>

I was trying to make some comparison so it could be exhibited, only I guess I got lost in logic.

  • Search today’s month in numbers, create two variables mesAnterior and mesSucessor, put their value as mesAtual -1 and mesAtual+1, makes a method that transfers the month from numbers to text, vualá

  • I understand, but nothing that can take advantage of this code from above? I was already creating the $y = date('n'); // Month 1 to 12 to see...

2 answers

1


See if that code helps you :)

//FUNCAO PARA ESCREVER MES POR EXTENSO
function mesExtenso($mes){
    if($mes == '01'){echo 'JANEIRO';}
    if($mes == '02'){echo 'FEVEREIRO';}
    if($mes == '03'){echo 'MARCO';}
    if($mes == '04'){echo 'ABRIL';}
    if($mes == '05'){echo 'MAIO';}
    if($mes == '06'){echo 'JUNHO';}
    if($mes == '07'){echo 'JULHO';}
    if($mes == '08'){echo 'AGOSTO';}
    if($mes == '09'){echo 'SETEMBRO';}
    if($mes == '10'){echo 'OUTUBRO';}
    if($mes == '11'){echo 'NOVEMBRO';}
    if($mes == '12'){echo 'DEZEMBRO';}
    echo "<br>";
}

//VERIFICANDO MES PASSADO
$mes_passado = new DateTime();
$mes_passado->modify('-1 months');
mesExtenso($mes_passado->format('m'));

//VERIFICANDO MES ATUAL
$mes_atual = new DateTime();
mesExtenso($mes_atual->format('m'));

//VERIFICANDO PROXIMO MES
$mes_futuro = new DateTime();
$mes_futuro->modify('+1 months');
mesExtenso($mes_futuro->format('m'));

As it is in May, this code returns:

ABRIL
MAIO
JUNHO
  • And how do I get the value in number even? Because you have to enter in the <option> the numeric value to send to bank. =\

  • 1

    Where it appears $mes_passado->format('m') it is with the number of the month, which is where I pass to the function return the month in full.

  • 1

    Very good, it seems that now everything worked out, thanks man!

0

To display only the three months (previous, current and next), you can create an array with range() that starts with the value of the current month minus one and goes up to the current month plus two, to print the full month use locale and strftime() together.

example - ideone.

Browser other questions tagged

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