Display calendar with PHP with certain conditions

Asked

Viewed 221 times

0

I’m starting my PHP studies ,and I have the following challenges: Show Sundays in red and Saturdays in bold. Make the calendar start on a day that is not a Sunday. but I have no idea how to do that, could anyone tell me how to do that? which logic to use.

    <?php
ini_set("display_errors",1);
ini_set("display_startup_erros",1);
error_reporting(E_ALL);
function linha($semana)
{
    echo"<tr>";
    for($i = 0; $i<=6;$i++){
        if(isset($semana[$i])){
            echo "<td>{$semana[$i]}</td>";
        }else{
            echo "<td></td>";
        }
    }
    echo "</tr>";
}
    function Calendario()
    {
        $dia = 1;
        $semana = array();
        while($dia <= 31) {
            array_push($semana, $dia);
            if (count($semana) == 7) {
                linha($semana);
                $semana = array();
            }
            $dia++;
        }
        linha($semana);
    }
?>
<table border="1">
    <tr>
        <th>Dom</th>
        <th>Seg</th>
        <th>Ter</th>
        <th>Qua</th>
        <th>Qui</th>
        <th>Sex</th>
        <th>Sáb</th>
    </tr>
    <?php  calendario();?>
</table>

1 answer

0

Well. The idea is to use the index of an array that contains the css settings: If you have something that is not clear in the code. Just ask that I edit the answer.

<?php
ini_set("display_errors",1);
ini_set("display_startup_erros",1);
error_reporting(E_ALL);
function linha($semana)
{
    $style = ['color:yellow ; background:red', '' , '', '' , '' , '' ,'font-weight: bold' ];
    echo"<tr>";
    for($i = 0; $i<=6;$i++){
        if(isset($semana[$i])){            
            echo '<td' . ' style="' . $style[$i] . '">';
            if($semana[$i] != 0){
                echo "{$semana[$i]}";
            }
            echo '</td>';
        }else{
            echo "<td></td>";
        }
    }
    echo "</tr>";
}
    function Calendario()
    {
        $dia = 0;
        $semana = array();
        while($dia <= 31) {
            array_push($semana, $dia);
            if (count($semana) == 7) {
                linha($semana);
                $semana = array();
            }
            $dia++;
        }
        linha($semana);
    }
?>
<table border="1">
    <tr>
        <th>Dom</th>
        <th>Seg</th>
        <th>Ter</th>
        <th>Qua</th>
        <th>Qui</th>
        <th>Sex</th>
        <th>Sáb</th>
    </tr>
    <?php  calendario();?>
</table>

Browser other questions tagged

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