Calendar in PHP

1

I have a calendar in php and I would like to know two things!

How do I get the date started on Monday? (Day 1 second, for example), and how to make Sundays appear in red.

Here is the code:

<!DOCTYPE HTML>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Calendário em PHP</title>
    <?php 
        date_default_timezone_set('America/Sao_Paulo');
    ?>
</head>
<body>
    <h1>Estamos em <?php echo date('Y');?></h1>
        <p>Hoje é dia <strong><?php echo date('d / '); ?></strong>
            <?php echo date('m'); ?>
            agora são <?php echo date ('H'); ?>horas e
            <?php echo date('i');?> minutos.</p>

        <?php
            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>
                <?php calendario(); ?>
            </tr>
        </table>
</body>
</html>
    
asked by anonymous 11.10.2014 / 01:00

2 answers

2

Try it out:

<!DOCTYPE HTML>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Calendário em PHP</title>
    <?php 
        date_default_timezone_set('America/Sao_Paulo');

        $hoje = getdate(strtotime($_GET['t']));

        $ultimoDia = cal_days_in_month(CAL_GREGORIAN,
                                       $hoje['mon'],
                                       $hoje['year']);

        $primeiraSemana = (($hoje['wday'] + 1) -
                          ($hoje['mday'] - ((int)($hoje['mday'] / 6) * 7))) % 7;
        // Alternativa:
        /*$primeiroDiaTimestamp = strtotime(sprintf("%d-%0d-01",
                                                  $hoje['year'],
                                                  $hoje['mon']));
        $primeiraSemana = (int)date('w', $primeiroDiaTimestamp);*/
    ?>

    <style>
        td[data-semana="0"] { color: #ff0000; }
    </style>
</head>
<body>
    <h1>Estamos em <?= $hoje['year'] ?></h1>
    <p><?= sprintf('Hoje é dia <strong>%0d / %0d</strong>, agora são %02d horas e %0d minutos.',
                   $hoje['mday'], $hoje['mon'], $hoje['hours'], $hoje['minutes'])
    ?></p>

    <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>
        <tr>
        <?php
        for($semana = 0; $semana < $primeiraSemana; ++$semana) {
            echo '<td>&nbsp;</td>';
        }
        for($dia = 1; $dia < $ultimoDia; ++$dia) {
            if( $semana > 6 ) {
                $semana = 0;
                echo '</tr><tr>';
            }

            echo "<td data-semana=\"$semana\">";
            echo "$dia</td>";
            ++$semana;
        }
        for(; $semana < 7; ++$semana) {
            echo '<td>&nbsp;</td>';
        }
        ?>
        </tr>
    </table>
</body>
</html>

I've put the CSS style in HTML, but it's good practice to put another CSS-only file.

I used the cal_days_in_month function to have the last day of the month.

To find out what day of the week, I showed you two solutions:

  • using the strtotime function to get the timestamp of the first day of the month, which can then be used in the date
  • doing an arithmetic calculation (I suppose the performance is better - it does not invoke two complex functions):

  • count the completed weekdays that have passed to date: (int)($hoje['mday'] / 6 ;
  • subtract today by the days of the complete weeks counted, giving the remaining days: $hoje['mday'] - ((int)($hoje['mday'] / 6) * 7)
  • subtract the number of the weekday plus one (Sunday is 0, second is 1, ...) for the previous result: ($hoje['wday'] + 1) - ($hoje['mday'] - ((int)($hoje['mday'] / 6) * 7))
  • For example, today is day 11 and Saturday. Only a full week passed (11/6 = 1.6 ...). One week has 7 days (1 * 7 = 7). It was 4 days after the 7 days (11 - 7 = 4). If the first day of the week was Sunday, a week or four more days would be spent on a Wednesday. It is necessary to pull the days to Saturday (7 days): 7 - 4 = 3 (a Wednesday).

The rest is easy:

  • lets pass the days that are not shown in the calendar.
  • shows the days of the month indicating the day of the week in the element "td"; when it is Saturday, the line ends (I assume you are using HTML5, which allows data-* attributes).
  • Finally, skip the remaining days until the end of the week (Saturday).
  • 11.10.2014 / 15:52
    0
    function linha($semana){
        $dias = count($semana);
        echo "<tr>";
        for ($i = 1; $i < $dias; $i++){
            if(isset($semana[$i])){
                echo "<td>{$semana[$i]}</td>";
            } else{
                echo "<td></td>";
            }
        }
        echo "<td class="vermelho">{$semana[0]}</td>";
        echo "</tr>";   }
    

    Complementing my previous comment, it would be something like this. I did not test the code because it is simple to implement, if you have something wrong you give a set, which is very simple.

        
    11.10.2014 / 02:59