Simple Calendar in PHP


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Kalender</title>
  6.  
  7. <style type="text/css">
  8. body{
  9. width: 300px;
  10. margin: 100px auto;
  11. background-color: #999999;
  12. text-align: center;
  13. font-family:"Trebuchet MS", arial, verdana;
  14. font-size: 12px;
  15. }
  16. #ContentBox{
  17. width: 250px;
  18. border: thin #333333 solid;
  19. }
  20.  
  21. table{
  22. width: 100%;
  23. border-collapse: collapse;
  24. background-color: #fffeff;
  25.  
  26. }
  27. th{
  28. background-color: #333333;
  29. color: #F4F4F4;
  30. font-size: 14px;
  31.  
  32. }
  33. tr.nav{
  34. border-bottom: thin #333333 solid ;
  35.  
  36. }
  37. a:link{
  38. text-decoration: none;
  39. color: black;
  40. }
  41. a:visited{
  42. text-decoration: none;
  43. color: black;
  44. }
  45. a:hover{
  46. text-decoration: none;
  47. color: white;
  48. }
  49. a:active{
  50. text-decoration: none;
  51. color: black;
  52. }
  53. .navig:link{
  54. font-weight: bold;
  55. display: block;
  56. text-decoration: none;
  57. }
  58. .navig:visited{
  59. font-weight: bold;
  60. background-color: #999999;
  61. text-decoration: none;
  62. }
  63. .navig:hover{
  64. font-weight: bold;
  65. background-color: #333333;
  66. text-decoration: none;
  67. color:white;
  68. }
  69. .navig:active{
  70. font-weight: bold;
  71. background-color: #333333;
  72. text-decoration: none;
  73. color:white;
  74. }
  75. .today{
  76. font-weight: bold;
  77. text-decoration: underline ;
  78. }
  79. .sunday{
  80. background-color: #CCCCCC;
  81. }
  82. .todaysunday{
  83. font-weight: bold;
  84. background-color: #990000;
  85. }
  86.  
  87. </style>
  88. </head>
  89.  
  90. <body>
  91.  
  92. <?php
  93.  
  94. function SmallCalender(){
  95. if (!isset($_GET["month"])){
  96. $_GET["month"] = date("n"); //n = Numeric representation of a month, without leading zeros
  97. $week_day = date("N");
  98. };
  99.  
  100. if (!isset($_GET["year"])){
  101. $_GET["year"] = date("Y"); // Y = A full numeric representation of a year, 4 digits
  102. };
  103.  
  104. if (!isset($_GET["week"])){
  105. $_GET["week"] = date("W"); // W = ISO-8601 week number of year, weeks starting on Monday
  106. };
  107. if (!isset($_GET["day"])){
  108. $_GET["day"] = date("j"); // j = Day of the month without leading zeros
  109. };
  110.  
  111. //Define current month, year and date
  112. $cMonth = $_GET["month"];
  113. $cYear = $_GET["year"];
  114. $cWeek = $_GET["week"];
  115. $cDay = $_GET["day"];
  116.  
  117. $today = date("jnY");
  118. $this_date = mktime(0, 0, 0, $cMonth, $cDay, $cYear);
  119.  
  120.  
  121.  
  122. //Calling to itself
  123. $self = @$_SERVER["PHP_SELF"];
  124.  
  125. //Variables for previous-next month and previous-next year
  126. $prev_year = $cYear;
  127. $next_year = $cYear;
  128. $prev_month = $cMonth - 1;
  129. $next_month = $cMonth + 1;
  130.  
  131. //Behaviour when months move from 1 to 12 and go to previous year
  132. if ($prev_month == 0){
  133. $prev_month = 12;
  134. $prev_year = $cYear - 1;
  135. };
  136.  
  137. //Behaviour when months move from 12 to 1 and go to next year
  138. if ($next_month == 13){
  139. $next_month = 1;
  140. $next_year = $cYear + 1;
  141. };
  142.  
  143. //Check last day of the current month
  144. $lastday = date("j", mktime(0, 0, 0 - 1, $cMonth + 1, 1, $cYear));
  145.  
  146. //Count days in current month
  147. $total_days = date("t", $this_date);
  148.  
  149. //Get numeric representation of day week ISO-8601
  150. $week_day = date("N", $this_date);
  151.  
  152. $daynames = array(
  153. "",
  154. "Man",
  155. "Tir",
  156. "Ons",
  157. "Tor",
  158. "Fre",
  159. "L&oslash;r",
  160. "S&oslash;n",
  161. );
  162.  
  163. $monthnames = array("",
  164. "Januar",
  165. "Februar",
  166. "Marts",
  167. "April",
  168. "Maj",
  169. "Juni",
  170. "Juli",
  171. "August",
  172. "September",
  173. "Oktober",
  174. "November",
  175. "December"
  176. );
  177. //---------- echoing year and month navigation -------------------------------------------------
  178.  
  179. echo "<table>";
  180.  
  181.  
  182. echo "<tr class=\"nav\">"; //upper row links to prev and next months
  183.  
  184. echo "<th colspan=\"6\">
  185. <strong>
  186. ".$monthnames[$cMonth]."&nbsp;".$cYear."
  187. </strong>
  188. </th>";
  189.  
  190. echo "<td >";
  191. echo "<a class=\"navig\" href='".$self."?month=".$prev_month."&year=".$prev_year."'>
  192. &lt;
  193. </a>
  194. </td><td>
  195. <a class=\"navig\" href = '".$self."?month=".$next_month."&year=".$next_year."'>
  196. &gt;
  197. </a>";
  198. echo "</td>";
  199. echo "</tr>";
  200.  
  201. echo "<tr>";
  202. echo "<td>
  203. Uge
  204. </td>";
  205. $cell = 1;
  206. while ($cell <= 7){
  207. if ($cell == 7){
  208. $style = " class=\"sunday\"";
  209. }else{
  210. $style = "";
  211. }
  212. echo "<td {$style}>";
  213. echo $daynames[$cell];
  214. echo "</td>";
  215. $cell++;
  216. }
  217. echo "</tr>";
  218.  
  219. echo "<tr>";
  220.  
  221.  
  222. //Print empty cells - Add weeknumbers
  223. $firstday = date("N", mktime(0, 0, 0, $cMonth, 1, $cYear));
  224. $w = date("W", mktime(0, 0, 0, $cMonth, 1, $cYear));
  225.  
  226. if ($firstday > 1){
  227. echo "<td> <a href='".$self."?week=".$w."&month=".$cMonth."&year=".$cYear."'>";
  228. echo $w;
  229. echo "</a></td>";
  230. for ($empty_cell = 1; $empty_cell < $firstday; $empty_cell++){
  231.  
  232. echo "<td>&nbsp;</td>";
  233. }
  234. }
  235. for ($d = 1; $d <= $lastday; $d++){
  236.  
  237. $N = date("N", mktime(0, 0, 0, $cMonth, $d, $cYear));
  238. $w = date("W", mktime(0, 0, 0, $cMonth, $d, $cYear));
  239. $j = date("jnY", mktime(0, 0, 0, $cMonth, $d, $cYear));
  240. if ($N == 7){
  241. $style = " class=\"sunday\"";
  242. }elseif ($j == $today){
  243.  
  244. $style = " class=\"today\"";
  245. }elseif ($j == $today && $N == 7){
  246.  
  247. $style = " class=\"todaysunday\"";
  248. }else{
  249. $style = " ";
  250. }
  251. if ($N == 1){
  252. echo "</tr><tr>";
  253. echo "<td {$style} ><a href='".$self."?week=".$w."&month=".$cMonth."&year=".$cYear."'>";
  254. echo $w;
  255. echo "</a></td>";
  256. }
  257.  
  258.  
  259. echo "<td {$style}> <a href='".$self."?day=".$d."&month=".$cMonth."&year=".$cYear."'>";
  260. echo $d;
  261. echo "</a></td>";
  262. }
  263.  
  264. echo "</tr>";
  265. echo "</table>";
  266.  
  267. $thismonth = date('n');
  268. $this_year = date("Y", time());
  269.  
  270. //Link to current date ****
  271. if ($cMonth <> $thismonth or $cYear <> $this_year){
  272. echo "<a class=\"a\" href='".$self."?month=".$thismonth."&year=".$this_year."'>
  273. Vis nuv&aelig;rende m&aring;ned
  274. </a>";
  275. }
  276. }
  277.  
  278. echo "<div id='ContentBox'>";
  279. SmallCalender();
  280. echo "</div>";
  281. ?>
  282.  
  283. </body>
  284. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.