PHP Calendar Navigation for Reports

Tonight I started working on creating a calendar. This calendar is needed so that I can show reports for the year, month, week, or day. It is easy to navigate between everything.

I can follow the greater than/less than signs to go to the next month or year. Clicking on the month, year, week number or day of month will select the appropriate items.

I did run into one problem. PHP sets the first day of the week to Mondays. So if I click on a week number, all the days for that week will be selected except Sunday. However, next weeks Sunday is selected. I need to find a way around this.

After this is done with, I should be able to create reports based on the selection and date provided.

   50 function drawCalendar() {
   51     $selection = $_GET[s];
   52     if($selection == null) $selection = “d”;
   53     $date = time();    if($_GET[d] != null) $date = date($_GET[d]);
   54     switch($selection) {
   55         case “y”:case “m”:case “w”:case “d”:break;
   56         default: $selection = “d”;}
   57     $m = date(“m”, $date); $y = date(“Y”, $date);
   58     $d = date(“d”, $date); $w = date(“W”, $date);
   59     $dy = date(“z”, $date);
   60     $ms = mktime(0, 0, 0, $m, 1, $y);
   61     $me = mktime(0, 0, 0, $m + 1, 1, $y);
   62     $wk = mktime(0, 0, 0, date(“m”, $ms), date(“d”, $ms) - date(“w”, $ms), date(“Y”, $ms));
   63     echo “<table border=\”1\” class=\”Calendar\” align=\”center\”>”;
   64     $selected = ($selection == “y” || $selection == “m”) ? “Selected” : “”;
   65     echo “<tr>”;
   66     $tmpDate = mktime(0, 0, 0, $m - 1, $d, $y);
   67     echo “<td colspan=\”2\”><a href=\”?d=$tmpDate&amp;s=$selection\”>&lt;</a></td>”;
   68     $tmpTitle = date(“F”, $date);
   69     echo “<td colspan=\”4\” class=\”Month$selected\”><a href=\”?d=$date&amp;s=m\”>$tmpTitle</a></td>”;
   70     $tmpDate = mktime(0, 0, 0, $m + 1, $d, $y);
   71     echo “<td colspan=\”2\”><a href=\”?d=$tmpDate&amp;s=$selection\”>&gt;</a></td>”;
   72     echo “</tr>”;
   73     $selected = ($selection == “y”) ? “Selected” : “”;
   74     echo “<tr>”;
   75     $tmpDate = mktime(0, 0, 0, $m, $d, $y - 1);
   76     echo “<td colspan=\”2\”><a href=\”?d=$tmpDate&amp;s=$selection\”>&lt;</a></td>”;
   77     $tmpTitle = date(“Y”, $date);
   78     echo “<td colspan=\”4\” class=\”Year$selected\”><a href=\”?d=$date&amp;s=y\”>$tmpTitle</a></td>”;
   79     $tmpDate = mktime(0, 0, 0, $m, $d, $y + 1);
   80     echo “<td colspan=\”2\”><a href=\”?d=$tmpDate&amp;s=$selection\”>&gt;</a></td>”;
   81     echo “</tr>”;
   82     echo “<tr><td>wk</td><td>S</td><td>M</td><td>T</td>”;
   83     echo “<td>W</td><td>T</td><td>F</td><td>S</td></tr>”;
   84     while($wk < $me) {
   85         echo “<tr>”;
   86         $wd = date(“d”, $wk); $wm = date(“m”, $wk);
   87         $wy = date(“Y”, $wk); $ww = date(“W”, $wk);
   88         $selected = ($selection == “y” && $wy == $y) || ($selection == “m” && $wm == $m) || ($selection == “w” && $ww == $w) ? “Selected” : “”;
   89         echo “<td class=\”Week$selected\”><a href=\”?d=$wk&amp;s=w\”>$ww</a></td>”;
   90         for($i = 0; $i < 7; $i++) {
   91             $dom = mktime(0, 0, 0, $wm, $wd + $i, $wy);
   92             $selected = ($selection == “y” && date(“Y”, $dom) == $y) || ($selection == “m” && date(“m”, $dom) == $m) || ($selection == “w” && date(“W”, $dom) == $w) || ($selection == “d” && date(“z”, $dom) == $dy) ? “Selected” : “”;
   93             $tmpTitle = date(“j”, $dom);
   94             echo “<td class=\”Day$selected\”><a href=\”?d=$dom&amp;s=d\”>$tmpTitle</a></td>”;
   95         }
   96         echo “</tr>”;
   97         $wk = mktime(0, 0, 0, $wm, $wd + 7, $wy);
   98     }
   99     echo “</table>”;
  100 }
  101 drawCalendar();

2 Responses to “PHP Calendar Navigation for Reports”

  1. François Says:

    Hi,

    Looking for a simple php calendar that I could adapt for my needs, I stepped on your nice code. Just what I need!

    Everything works, except this week number problem. Independantly about what you said from the start of the week, could you tell me how to set it to display the begining on Monday instead of Sunday.

    Thanks a lot for your advice

    François

  2. François Says:

    Hi again,

    I think after a few tries, that I got it right, I’ll send you the adress of the code I developped for mediawiki (the wikipedia engine).

    Regards

    François

Leave a Reply