PHP语言的日历

时间:2014-10-10 23:03:59

标签: php calendar

我在php中有一个日历,我只想知道两件简单的事情!

如何在不是星期日的一天开始日历? (例如,第1天=星期一,例如)。

如何使所有星期日都以红色显示?

Thx,这是代码:

<!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>

2 个答案:

答案 0 :(得分:0)

函数date()提供了很多东西来比较星期几。显然,你需要告诉PHP参考的日期,以便PHP知道它是什么日子。

在您的示例中,您可以设置引用的时间戳,并使用日期('N',$ current_timestamp):

  • 您可以与循环中的星期几(1 =星期一到7 =星期日)进行比较,使日历仅在您想要的日期开始。
  • 您可以查看==到7(星期日)
  • 您可以查看是否是今天这一天

我使用这个函数重写了日历函数:

    <?php

$calendar = "";
$current_time = mktime(0, 0, 0, date("n"), 1) - 604800; // say, we start at the first day of the current month.


$days = "";


    for ($w = 1 ; $w <=5 ; $w++) // how many weeks to display.
    {
        $week = "";
        $days = "";

        for ($j = 1 ; $j <=7 ; $j++) // for each day slot in your week
        {

            if($j != date('N', $current_time)) 
            {
                $days .= "";    // if the current looped day is not corresponding to the day in the calendar column, then empty slot and we don't increment the day until it matches.
                $j=$j-1;
            }
            else if(date('Ymd') == date('Ymd', $current_time))
            {
                $days .= "<td style=\"background-color:yellow;\">" . date('j', $current_time) . "</td>\n"; // if the day is today, then print in the calendar in yellow.
            }
            else if ($j == 7)
            {
                $days .= "<td style=\"background-color:red;\">" . date('j', $current_time) . "</td>\n"; // if Sunday, then in red.
            }
            else
            {
                $days .= "<td>" . date('j', $current_time) . "</td>\n"; // if the day corresponds but is not today, then print in the calendar (white).
            }

            $current_time = $current_time + 86400;
        }

        $week .= "<tr>" . $days . "</tr>\n";

        $calendar .=  $week;
    }


echo ("<table cellspacing=\"2\" border=\"1\">
\t<tr>
\t<th>M</th>
\t<th>T</th>
\t<th>W</th>
\t<th>T</th>
\t<th>F</th>
\t<th>S</th>
\t<th>S</th>
</tr>" . $calendar . "</table>");

?>

答案 1 :(得分:0)

这是我的解决方案:

<!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();

        $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>

使用strtotime和date你可以得到这个月第一天的工作日:

$firstWeekdayOfTheMonth = date('w', strtotime('first day of this month'));

我提出的解决方案使用算术计算来查找当月的第一个工作日,知道当前的月份和工作日:

$today = getdate();
$firstWeekdayOfTheMonth = (($today['wday'] + 1) - ($today['mday'] - ((int)($today['mday'] / 6) * 7))) % 7;

计算本月通过的整周(7天)的日期:例如,今天是该月的第11天,因此这整整一周(11/6 = 1.6 ......)已经过去了。一周有7天。

接下来,它会计算本月过去的天数与之前计算的天数之间的差异。 11 - 7 = 4.如果该月的第一天是星期日,则今天是星期四(自星期一起7天加上4天)。为了获得正确的工作日,需要移动正确的日期(使用当前工作日的数量):7 - 4 = 3(它是7,因为它是自第7天以来的第7天星期天; 3代表星期三。)