SELECT HTML field displaying Previous, current, and next month only

2

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

I'm getting system ready to modify here at the stage what I currently have code for 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 that it could be displayed, but I think I got lost in logic.

    
asked by anonymous 22.05.2015 / 13:56

2 answers

1

See if this 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 we are in May, this code returns:

ABRIL
MAIO
JUNHO
    
22.05.2015 / 14:35
0

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

example - ideone .

    
22.05.2015 / 14:34