Days of the week dynamically

Asked

Viewed 304 times

-1

I want to show the days of the week (Tuesday 2 December) in a table per week from the date function. That is, the table has 7 columns and starts on Sunday until Saturday. For example:

<td>Dia da semana</td>

1 answer

1

You can make use of the function date() PHP to get the day of the week.

echo date("l"); //exibe o nome do dia da semana, em inglês
echo date("N"); //exibe a representação numérica do dia da semana

echo date("d")." "; //exibe o dia
echo date("m")." "; //exibe o mês
echo date("Y")." "; //exibe o ano

As parameter you pass the desired output format ("N" returns the ISO-8601 numeric representation of the day of the week)

Using date("N") it is possible to construct a simple function that returns the day of the week in Portuguese.

echo dayOfWeek(date("N")); //exibe o dia da semana em portugues

function dayOfWeek($day){
    switch ($day) {
        case 1:
            return "Segunda";
        case 2:
            return "Terça";
        case 3:
            return "Quarta";
        case 4:
            return "Quinta";
        case 5:
            return "Sexta";
        case 6:
            return "Sábado";
        case 7:
            return "Domingo";
    }
}

On the question of generating a table with days of the week...

echo "<table>";
echo "<tr>";
for ($i=1; $i <= 7 ; $i++) { 
    echo "<td>";
    echo dayOfWeek($i);
    echo "</td>";
}
echo "</tr>";
echo "</table>";
  • Right and I want to write the day and the month next to the week day.

  • 1

    same scheme, only search in the documentation which parameter returns the month number

Browser other questions tagged

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