Return to Snippet

Revision: 32034
at September 17, 2010 20:43 by zafu


Initial Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Kalender</title>

        <style type="text/css">
            body{
                width: 300px;
                margin: 100px auto;
                background-color: #999999;
                text-align: center;
                font-family:"Trebuchet MS", arial, verdana;
                font-size: 12px;
            }
            #ContentBox{
                width: 250px;
                border: thin #333333 solid;
            }

            table{
                width: 100%;
                border-collapse: collapse;
                background-color:  #fffeff;

            }
            th{
                background-color: #333333;
                color: #F4F4F4;
                font-size: 14px;

            }
            tr.nav{
                border-bottom: thin #333333 solid ;

            }
            a:link{
                text-decoration: none;
                color: black;
            }
            a:visited{
                text-decoration: none;
                color: black;
            }
            a:hover{
                text-decoration: none;
                color: white;
            }
            a:active{
                text-decoration: none;
                color: black;
            }
            .navig:link{
                font-weight: bold;
                display: block;
                text-decoration: none;
            }
            .navig:visited{
                font-weight: bold;
                background-color: #999999;
                text-decoration: none;
            }
            .navig:hover{
                font-weight: bold;
                background-color: #333333;
                text-decoration: none;
                color:white;
            }
            .navig:active{
                font-weight: bold;
                background-color: #333333;
                text-decoration: none;
                color:white;
            }
            .today{
                font-weight: bold;
                text-decoration: underline ;
            }
            .sunday{
                background-color: #CCCCCC;
            }
            .todaysunday{
                font-weight: bold;
                background-color: #990000;
            }

        </style>
    </head>

    <body>

        <?php

        function SmallCalender(){
            if (!isset($_GET["month"])){
                $_GET["month"] = date("n"); //n = Numeric representation of a month, without leading zeros
                $week_day = date("N");
            };

            if (!isset($_GET["year"])){
                $_GET["year"] = date("Y"); // Y = A full numeric representation of a year, 4 digits
            };

            if (!isset($_GET["week"])){
                $_GET["week"] = date("W"); // W = ISO-8601 week number of year, weeks starting on Monday
            };
            if (!isset($_GET["day"])){
                $_GET["day"] = date("j"); // j = Day of the month without leading zeros
            };

            //Define current month, year and date
            $cMonth = $_GET["month"];
            $cYear = $_GET["year"];
            $cWeek = $_GET["week"];
            $cDay = $_GET["day"];

            $today = date("jnY");
            $this_date = mktime(0, 0, 0, $cMonth, $cDay, $cYear);

            

            //Calling to itself
            $self = @$_SERVER["PHP_SELF"];

            //Variables for previous-next month and previous-next year
            $prev_year = $cYear;
            $next_year = $cYear;
            $prev_month = $cMonth - 1;
            $next_month = $cMonth + 1;

            //Behaviour when months move from 1 to 12 and go to previous year
            if ($prev_month == 0){
                $prev_month = 12;
                $prev_year = $cYear - 1;
            };

            //Behaviour when months move from 12 to 1 and go to next year
            if ($next_month == 13){
                $next_month = 1;
                $next_year = $cYear + 1;
            };

            //Check last day of the current month
            $lastday = date("j", mktime(0, 0, 0 - 1, $cMonth + 1, 1, $cYear));

            //Count days in current month
            $total_days = date("t", $this_date);

            //Get numeric representation of day week ISO-8601
            $week_day = date("N", $this_date);

            $daynames = array(
                "",
                "Man",
                "Tir",
                "Ons",
                "Tor",
                "Fre",
                "L&oslash;r",
                "S&oslash;n",
            );

            $monthnames = array("",
                "Januar",
                "Februar",
                "Marts",
                "April",
                "Maj",
                "Juni",
                "Juli",
                "August",
                "September",
                "Oktober",
                "November",
                "December"
            );
            //---------- echoing year and month navigation -------------------------------------------------

            echo "<table>";


            echo "<tr class=\"nav\">"; //upper row links to prev and next months

            echo "<th colspan=\"6\">
                    <strong>
                    ".$monthnames[$cMonth]."&nbsp;".$cYear."
                    </strong>
                    </th>";

            echo "<td >";
            echo "<a class=\"navig\" href='".$self."?month=".$prev_month."&year=".$prev_year."'>
                    &lt;
                    </a>
                    </td><td>
                    <a class=\"navig\" href = '".$self."?month=".$next_month."&year=".$next_year."'>
                    &gt;
                    </a>";
            echo "</td>";
            echo "</tr>";

            echo "<tr>";
            echo "<td>
                    Uge
                    </td>";
            $cell = 1;
            while ($cell <= 7){
                if ($cell == 7){
                    $style =  " class=\"sunday\"";
                }else{
                    $style = "";
                }
                echo "<td {$style}>";
                echo $daynames[$cell];
                echo "</td>";
                $cell++;
            }
            echo "</tr>";

            echo "<tr>";


            //Print empty cells - Add weeknumbers
            $firstday = date("N", mktime(0, 0, 0, $cMonth, 1, $cYear));
            $w = date("W", mktime(0, 0, 0, $cMonth, 1, $cYear));

            if ($firstday > 1){
                echo "<td> <a href='".$self."?week=".$w."&month=".$cMonth."&year=".$cYear."'>";
                echo $w;
                echo "</a></td>";
                for ($empty_cell = 1; $empty_cell < $firstday; $empty_cell++){

                    echo "<td>&nbsp;</td>";
                }
            }
            for ($d = 1; $d <= $lastday; $d++){

                $N = date("N", mktime(0, 0, 0, $cMonth, $d, $cYear));
                $w = date("W", mktime(0, 0, 0, $cMonth, $d, $cYear));
                $j = date("jnY", mktime(0, 0, 0, $cMonth, $d, $cYear));
                if ($N == 7){
                    $style = " class=\"sunday\"";
                }elseif ($j == $today){

                    $style = " class=\"today\"";
                }elseif ($j == $today && $N == 7){

                    $style = " class=\"todaysunday\"";
                }else{
                    $style = " ";
                }
                if ($N == 1){
                    echo "</tr><tr>";
                    echo "<td {$style} ><a href='".$self."?week=".$w."&month=".$cMonth."&year=".$cYear."'>";
                    echo $w;
                    echo "</a></td>";
                }


                echo "<td {$style}> <a href='".$self."?day=".$d."&month=".$cMonth."&year=".$cYear."'>";
                echo $d;
                echo "</a></td>";
            }

            echo "</tr>";
            echo "</table>";

            $thismonth = date('n');
            $this_year = date("Y", time());
            
            //Link to current date ****
            if ($cMonth <> $thismonth or $cYear <> $this_year){
                echo "<a class=\"a\" href='".$self."?month=".$thismonth."&year=".$this_year."'>
                Vis nuv&aelig;rende m&aring;ned
                </a>";
            }
        }

        echo "<div id='ContentBox'>";
        SmallCalender();
        echo "</div>";
        ?>

    </body>
</html>

Initial URL


Initial Description
Shows a month calendar, forward and backward navigation by month. Week numbers are shown at left col. Days and weeks are clickable. Days, weeks and months names are in Danish. Comments and suggestions are welcome

Initial Title
Simple Calendar in PHP

Initial Tags
php

Initial Language
PHP