How to get the name of the month in an input type=Month?

Asked

Viewed 781 times

-1

I have a code that has an input of type Month that generates a calendar with the values passed in the input, but it saves the values in this standard 2000-12, and I want to display the name of the month on top of the calendar but it is not possible because the month is passed in digits, there is some way to take the values directly from the input?

<body>
        
        <?php
        
        $datee = explode('-', $_POST['datac']);
        $mes = $datee[1];
        $ano = $datee[0];
        $ultimo_dia = date("t", mktime(0,0,0,$mes,'01',$ano));
        if ($mes == date('m')){
            $dias = $ultimo_dia;
        }elseif ( $mes == '') {
            $mes = date('m');
            $ano = date('o');
            $dias = $ultimo_dia;
        }else{
            $dias = $ultimo_dia;
        }
    

            ?>
            <?php
            echo '<h4>'.$mes. ' de ' .$ano.'</h4>';
            ?>
    
            <form method="post" action="date.php">
                <input type="month" name="datac" value="<?php echo $ano?>-<?php echo $mes?>" required><input type="submit">
                <table class="table table-striped" width="210" border="2" cellspacing="4" cellpadding="4">
                    <tr>
                        <td width="80px"><center>Domingo</center></td>
                        <td width="80px"><center>Segunda</center></td>
                        <td width="80px"><center>Terça</center></td>
                        <td width="80px"><center>Quarta</center></td>
                        <td width="80px"><center>Quinta</center></td>
                        <td width="80px"><center>Sexta</center></td>
                        <td width="80px"><center>Sábado</center></td>
                    </tr>
                    <?php
                    echo "<tr>";
                    for ($i = 1; $i <= $dias; $i++) {
                        $diadasemana = date("w", mktime(0, 0, 0, $mes, $i, $ano));
                        $cont = 0;
                        if ($i == 1) {
                            while ($cont < $diadasemana) {
                                echo "<td></td>";
                                $cont++;
                            }
                        }
                        echo "<td width='100px' height='100px'><center>";
                        echo $i;
                        echo "</center></td>";
                        if ($diadasemana == 6) {
                            echo "</tr>";
                            echo "<tr>";
                        }
                    }
                    echo "</tr>";
                    ?>
            </table>
                
    
                    </form>
                    </body>
                    </html>

  • Paste your code here if possible

  • I’ve already put it, it’s giving an error because of the indefinite variables, but it works normally.

  • Let me get this straight, you want to show the month name instead of the month number ?

  • That’s right :D!!

1 answer

2


Below I will cite some possible ways =>

    <?php
    ############################################################
    ############## Essas configurações podem ser settadas no seu servidor Web
    ###########################################################
    setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
    date_default_timezone_set('America/Sao_Paulo');


    $getMonthNum = 06;
    ############################################################

    echo strftime("%B", strtotime(date("Y-{$getMonthNum}-d")));

    ############################################################
    echo '<br>';
    $dateObj = DateTime::createFromFormat('!m', $getMonthNum);
    $mes = $dateObj->format('F');

    echo strftime('%B', $dateObj->getTimestamp());

    ##############################################################
    echo '<br>';
    $meses = array(1 => "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho",
        7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => "Dezembro");

    echo $meses[$getMonthNum];

Something else, instead of using <center>, use style="text-align:center". Because <center> already this DEPRECATED in HTML5

  • Valeu Lucaoa!!!!!!

  • @Nikk17016 arrange. But if it was useful, help me with a heheh vote

Browser other questions tagged

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