Create table with days of the month

Asked

Viewed 568 times

0

I’m creating a table with the days of each month in this way:

<?php
function nome_mes($num){
$mes = '';
switch ($num) {
case 0:
continue;
case 1:
$mes = "JANEIRO";
break;
case 2:
$mes = "FEVEREIRO";
break;
case 3:
$mes = "MARÇO";
break;
case 4:
$mes = "ABRIL";
break;
case 5:
$mes = "MAIO";
break;
case 6:
$mes = "JUNHO";
break;
case 7:
$mes = "JULHO";
break;
case 8:
$mes = "AGOSTO";
break;
case 9:
$mes = "SETEMBRO";
break;
case 10:
$mes = "OUTUBRO";
break;
case 11:
$mes = "NOVEMBRO";
break;
case 12:
$mes = "DEZEMBRO";
break;
}
return $mes;
}

//salva em um array qtos dias tem no determinado mês
$array_num_dias = Array(); 
for($i = 1; $i <= 12; $i++ ){
$array_num_dias[$i] = cal_days_in_month(CAL_GREGORIAN, $i, 2018);
}

//cria TD colspan dos meses
echo '<table>';
echo '<tr>';
for($i = 1; $i <= 12; $i++ ){
echo '<td colspan="' . $array_num_dias[$i] . '">'.nome_mes($i).'</td>';
}
echo '</tr>';

//cria TD dos dias
echo '<tr>';
for($i = 1; $i <= 12; $i++ ){
for($j = 1; $j <= $array_num_dias[$i];$j++){
echo '<td>' . $j . '</td>';
}  
}
echo '</tr>';
?>

The result is this: inserir a descrição da imagem aqui

I wanted to be able to select the month I want to view and not all at the same time. I would also like to ask if I am doing the best way, to create the table with the days of each month

  • Just use a value via POST or GET and use the if when listing the months. Ex: if ($_GET['month'] == $i) { /* Exibe a tabela */ }

  • @Valdeir Psr, can put an example of how to apply this if?

1 answer

1


Just delete the loops

for($i = 1; $i <= 12; $i++ ){

and create a variable $i with the amount equal to the desired month:

PHP

if (isset($_POST['mes'])){

$i=$_POST['mes'];

    function nome_mes($num){
    $mes = '';
    switch ($num) {
    case 0:
    continue;
    case 1:
    $mes = "JANEIRO";
    break;
    case 2:
    $mes = "FEVEREIRO";
    break;
    case 3:
    $mes = "MARÇO";
    break;
    case 4:
    $mes = "ABRIL";
    break;
    case 5:
    $mes = "MAIO";
    break;
    case 6:
    $mes = "JUNHO";
    break;
    case 7:
    $mes = "JULHO";
    break;
    case 8:
    $mes = "AGOSTO";
    break;
    case 9:
    $mes = "SETEMBRO";
    break;
    case 10:
    $mes = "OUTUBRO";
    break;
    case 11:
    $mes = "NOVEMBRO";
    break;
    case 12:
    $mes = "DEZEMBRO";
    break;
    }
    return $mes;
    }
    
    
    //salva em um array qtos dias tem no determinado mês
    $array_num_dias = Array(); 
    
    $array_num_dias[$i] = cal_days_in_month(CAL_GREGORIAN, $i, 2018);
    
    
    //cria TD colspan dos meses
    echo '<table>';
    echo '<tr>';
    
    echo '<td colspan="' . $array_num_dias[$i] . '">'.nome_mes($i).'</td>';
    
    echo '</tr>';
    
    //cria TD dos dias
    echo '<tr>';
    
    for($j = 1; $j <= $array_num_dias[$i];$j++){
    echo '<td>' . $j . '</td>';
    }  
    
    echo '</tr></table>';

}
?>

HTML

<form method="post" action="">
    <input type="number" name="mes" min="1" max="12" step="1">
    <input type="submit" name="Inserir" value="gerar">
</form>

PHP

if (isset($_POST['mes'])){

    $i=$_POST['mes'];
    $a=$_POST['ano'];
    
    //Define a localidade de determinado país.
    setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
    date_default_timezone_set('America/Sao_Paulo');
    
    //Retorna um novo objeto DateTime formatado de acordo com um formato informado
    $dateObj   = DateTime::createFromFormat('m', $i);
    $nome_mes = strftime( '%B', $dateObj -> getTimestamp() );
    
    
    //salva em um array qtos dias tem no determinado mês
    $array_num_dias = Array(); 
    
    $array_num_dias[$i] = cal_days_in_month(CAL_GREGORIAN, $i, $a);
    
    
    //cria TD colspan dos meses
    echo '<table>';
    echo '<tr>';
    
    echo '<td colspan="' . $array_num_dias[$i] . '">'.strtoupper($nome_mes).'/'.$a.'</td>';
    
    echo '</tr>';
    
    //cria TD dos dias
    echo '<tr>';
    
    for($j = 1; $j <= $array_num_dias[$i];$j++){
    echo '<td>' . $j . '</td>';
    }  
    
    echo '</tr></table>';


}

HTML

<form method="post" action="">
Mês <input type="number" name="mes" min="1" max="12" step="1"><br>
Ano <input type="number" name="ano" min="1900" max="2099" step="1" value="2018" /><br>
    <input type="submit">
</form>

Client-side validations are made but it would be convenient to also do server-side validations.

Datetime::createFromFormat

  • works, but instead of being numbers to put in the input, appear month names, it is possible?

  • @Better beginner, in this case, I think, a select

  • I’m creating these tables with the days of the months to then generate a shift time, think I’m following the best path?

  • @Beginner, then just seeing the full code

  • I still only have this created in code, now it is to create a table in mysql with the possible cycles for the schedules and then wanted to insert the name of the collaborator in the table of each month fill in the schedule right for the full month. Or isn’t this the best process? It’s hard to find a way to create shift schedules

  • @Beginner, the best thing to do is to try to create some code in this sense and if there is any difficulty creating new post with the difficulties encountered. In this post here both the question and the answer are well defined and any other condition will lose focus.

Show 2 more comments

Browser other questions tagged

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