PHP simple calendar


SUBMITTED BY: Guest

DATE: Oct. 23, 2013, 5:55 p.m.

FORMAT: Text only

SIZE: 8.2 kB

HITS: 890

  1. <?php
  2. /*
  3. Yves Glodt (yg@mind.lu) 13.01.2002
  4. This is based on a calendar I found in the phpbuilder code library (http://phpbuilder.com/snippet/detail.php?type=snippet&id=22)
  5. It still looks kinda similar, but behind the scenes, much has changed.
  6. Some description is available at http://www.mind.lu/~yg/stuff/
  7. 19.02.2002 - Code cleanup and fixed error in getting the number of days in a month.
  8. Ver 2.0 Changes
  9. - Added Remarks for the Newbies like me.
  10. - Made changes to the format and color.
  11. - Added a Next and Previous button.
  12. - Corrected the FORM so it works. I had issues with it not submiting.
  13. Next I will be adding a database function to it.
  14. Changes:
  15. ---------
  16. - The html actually validates.
  17. - Changed form-method from "post" to "get", to simplify integration.
  18. - All dates are handled correctly, the result looks always good and creates valid html.
  19. - When the current month/year is shown, the current day is highlighted.
  20. - The years-<select> is dynamic, it shows the selected year +/- 5 years.
  21. - A light version, for embedding into existing pages is available. (http://www.mind.lu/~yg/stuff/)
  22. Note:
  23. The valid range of a timestamp is typically from
  24. Fri, 13 Dec 1901 20:45:54 GMT to
  25. Tue, 19 Jan 2038 03:14:07 GMT.
  26. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)
  27. */
  28. $CurDate = getdate(); //gets the server current date.
  29. if (checkdate($HTTP_GET_VARS['month'],1,$HTTP_GET_VARS['year']) == NULL)
  30. {
  31. $YearToShow = $CurDate['year']; // $YearToShow = $CurDate['year'];
  32. $MonthToShow = $CurDate['mon']; // $MonthToShow = $CurDate['mon'];
  33. }
  34. else
  35. {
  36. if (checkdate($HTTP_GET_VARS['month'],1,$HTTP_GET_VARS['year']) == false)
  37. {
  38. $YearToShow = $CurDate['year'];
  39. $MonthToShow = $CurDate['mon'];
  40. }
  41. else
  42. {
  43. $YearToShow = $HTTP_GET_VARS['year'];
  44. $MonthToShow = $HTTP_GET_VARS['month'];
  45. if ( ($YearToShow < 1902) || ($YearToShow > 2037) )
  46. {
  47. $YearToShow = $CurDate['year'];
  48. $MonthToShow = $CurDate['mon'];
  49. }
  50. }
  51. }
  52. // This checks to see if the current day will be displayed. If it is make the background a color.
  53. if ( ($YearToShow == $CurDate['year']) && ($MonthToShow == $CurDate['mon']) ) { $DayToShow = $CurDate['mday']; }
  54. // This checks to see how many days are in the month in question.
  55. $NumberOfDays = date(t,mktime(0,0,0,$MonthToShow+1,0,$YearToShow,-1));
  56. // This section converts the month number to the month name.
  57. $MonthNames = array(1=>'January','February','March','April','May','June','July','August','September','October','November','December');
  58. //$Years = array('1998','1999','2000','2001','2002','2003','2004','2005');
  59. $Years = array($YearToShow-5,$YearToShow-4,$YearToShow-3,$YearToShow-2,$YearToShow-1,$YearToShow,$YearToShow+1,$YearToShow+2,$YearToShow+3,$YearToShow+4,$YearToShow+5);
  60. // Sets up the href
  61. if ($MonthToShow == 12)
  62. {
  63. $pmon = ($MonthToShow - 1);
  64. $pyear = ($YearToShow);
  65. $nmon = (1);
  66. $nyear = ($YearToShow + 1);
  67. }
  68. else
  69. {
  70. if ($MonthToShow == 1)
  71. {
  72. $pmon = (12);
  73. $pyear = ($YearToShow - 1);
  74. $nmon = ($MonthToShow + 1);
  75. $nyear = ($YearToShow);
  76. }
  77. else
  78. {
  79. $pmon = ($MonthToShow - 1);
  80. $pyear = ($YearToShow);
  81. $nmon = ($MonthToShow + 1);
  82. $nyear = ($YearToShow);
  83. }
  84. }
  85. echo <<<EOT
  86. <table border="1" cellpadding="0" cellspacing="0" bordercolor=black>
  87. <tr><td colspan="7" align="center">
  88. <table width="100%" border="0" cellspacing="0" cellpadding="2">
  89. <tr>
  90. <td width="20%"><font color="#000000" size="4" face="Times New Roman, Times, serif">$MonthNames[$MonthToShow] $YearToShow</font></td>
  91. <td width="30%" align="right">
  92. <a href=$PHP_SELF?month=$pmon&year=$pyear><< previous</a>
  93. &nbsp;&nbsp;&nbsp;&nbsp;
  94. <a href=$PHP_SELF?month=$nmon&year=$nyear>next >></a>
  95. </td>
  96. <td align="right" width="50%"><form action="$PHP_SELF" method="get">
  97. <font size="2" face="Times New Roman, Times, serif">Month</font><select name="month">
  98. EOT;
  99. while (list($key,$value) = each($MonthNames))
  100. {
  101. if ($key != $MonthToShow) { print '<option value="'.$key.'">'.$value."</option>\n"; }
  102. else { print '<option value="'.$key.'" selected>'.$value."</option>\n"; }
  103. }
  104. print "</select>\nYear<select name=\"year\">\n";
  105. while (list($key,$value) = each($Years))
  106. {
  107. if ($value != $YearToShow) { print '<option value="'.$value.'">'.$value."</option>\n"; }
  108. else { print '<option value="'.$value.'" selected>'.$value."</option>\n"; }
  109. }
  110. echo <<<EOT
  111. </select>
  112. <input type="submit" value="Go"></td>
  113. </tr>
  114. </table>
  115. </td></tr>
  116. <tr>
  117. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Sunday</strong></font></td>
  118. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Monday</strong></font></td>
  119. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Tuesday</strong></font></td>
  120. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Wednesday</strong></font></td>
  121. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Thursday</strong></font></td>
  122. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Friday</strong></font></td>
  123. <td width="100" align="center" bgcolor="#CC0000"><font color="#FFFFFF"><strong>Saturday</strong></font></td>
  124. </tr>
  125. EOT;
  126. $FirstDayOfWeek = date(l,mktime(0,0,0,$MonthToShow,1,$YearToShow));
  127. // This section ofsets the first day of the month so it matches the day of week.
  128. switch ($FirstDayOfWeek) {
  129. case 'Monday':
  130. $offset = 1;
  131. break;
  132. case 'Tuesday':
  133. $offset = 2;
  134. break;
  135. case 'Wednesday':
  136. $offset = 3;
  137. break;
  138. case 'Thursday':
  139. $offset = 4;
  140. break;
  141. case 'Friday':
  142. $offset = 5;
  143. break;
  144. case 'Saturday':
  145. $offset = 6;
  146. break;
  147. default:
  148. $offset = 0;
  149. }
  150. // this covers the first few empty days.
  151. if ($offset > 0) {
  152. print "<tr height=100 valign=top>";
  153. echo str_repeat("<td bgcolor=000000>&nbsp;</td>",$offset);
  154. }
  155. // This section is to Allow the first day of the week to be sunday and also.
  156. // to make sure that the table prints out right.
  157. for ($i=1; $i <= $NumberOfDays; $i++) {
  158. $DayOfWeek = date(l,mktime(0,0,0,$MonthToShow,$i,$YearToShow));
  159. if($DayOfWeek == 'Sunday') {
  160. print "<tr height=100 valign=top>";
  161. }
  162. if ($i != $DayToShow) {
  163. print "<td>$i</td>";
  164. } else {
  165. print "<td bgcolor=yellow>$i</td>";
  166. }
  167. if($DayOfWeek == 'Saturday') {
  168. print "</tr>\n";
  169. }
  170. }
  171. // This section will fill in the blank spaces.
  172. // The first part covers Feb.
  173. if ( ( ($offset == 5) && ($NumberOfDays > 30) ) || ( ($offset == 6) && ($NumberOfDays > 29) ) ) {
  174. if (42-$NumberOfDays-$offset > 0) {
  175. echo str_repeat("<td bgcolor=000000 class=\"n\">&nbsp;</td>\n",42-$NumberOfDays-$offset);
  176. }
  177. print "</tr>\n";
  178. } elseif ( ($NumberOfDays != 28) || ($offset > 0) ) {
  179. if (35-$NumberOfDays-$offset > 0) {
  180. echo str_repeat("<td bgcolor=000000 class=\"n\">&nbsp;</td>\n",35-$NumberOfDays-$offset);
  181. print "</tr>\n";
  182. }
  183. }
  184. echo <<<EOT
  185. </table>
  186. EOT;
  187. ?>

comments powered by Disqus