calendar.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <html>
  2. <head>
  3. <link href="calendar.css?ver=123" type="text/css" rel="stylesheet" />
  4. </head>
  5. <body>
  6. <?php
  7. echo '<p align="center">';
  8. $mydate = date('Y-m-d');
  9. if(isset($_POST['mydate']))
  10. {
  11. $mydate = $_POST['mydate'];
  12. }
  13. echo "Date: $mydate";
  14. echo "</p>\n";
  15. $calendar = new Calendar($mydate);
  16. echo $calendar->show();
  17. ?>
  18. </body>
  19. </html>
  20. <!-- class Calendar -->
  21. <?php
  22. date_default_timezone_set('Europe/Vienna');
  23. class Calendar
  24. {
  25. // Constructor
  26. public function __construct($selectedDate)
  27. {
  28. $this->selectedDate=$selectedDate;
  29. $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
  30. }
  31. // Properties
  32. private $dayLabels = array("MO","TU","WE","TH","FR","SA","SU");
  33. private $currentYear=0;
  34. private $currentMonth=0;
  35. private $currentDay=0;
  36. private $currentDate=null;
  37. public $selectedDate;
  38. private $currentWeek=0;
  39. private $daysInMonth=0;
  40. private $naviHref= null;
  41. // print out the calendar
  42. public function show()
  43. {
  44. $year = null;
  45. $month = null;
  46. if(null==$year&&isset($_GET['year']))
  47. {
  48. $year = $_GET['year'];
  49. }
  50. else if(null==$year)
  51. {
  52. $year = date("Y",time());
  53. }
  54. if(null==$month&&isset($_GET['month']))
  55. {
  56. $month = $_GET['month'];
  57. }
  58. else if(null==$month)
  59. {
  60. $month = date("m",time());
  61. }
  62. $this->currentYear=$year;
  63. $this->currentMonth=$month;
  64. $this->daysInMonth=$this->_daysInMonth($month,$year);
  65. $weeksInMonth = $this->_weeksInMonth($month,$year);
  66. $content="\n<div class=\"calendar\">\n".
  67. $this->_createNavi().
  68. "<table class=\"calendar\">\n".
  69. "<form method=\"post\" action=\"\">\n".
  70. $this->_createLabels();
  71. // Create weeks in a month
  72. for( $i=0; $i<$weeksInMonth; $i++ )
  73. {
  74. $content.="\t<tr>\n";
  75. //Create days in a week
  76. for($j=1;$j<=7;$j++)
  77. {
  78. $content.=$this->_showDay($i*7+$j);
  79. }
  80. $content.="\t</tr>\n";
  81. }
  82. $content.="</form>\n";
  83. $content.="</table>\n";
  84. $content.="</div>\n";
  85. return $content;
  86. }
  87. // get single of a day
  88. private function _showDay($cellNumber)
  89. {
  90. $day="";
  91. if($this->currentDay==0)
  92. {
  93. $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));
  94. if(intval($cellNumber) == intval($firstDayOfTheWeek))
  95. {
  96. $this->currentDay=1;
  97. }
  98. }
  99. if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) )
  100. {
  101. $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));
  102. $cellContent = $this->currentDay;
  103. $this->currentDay++;
  104. $this->currentWeek=date("W", strtotime($this->currentDate));
  105. if ($this->currentDate != $this->selectedDate)
  106. {
  107. $day="\t\t<td><button type=\"submit\" name=\"mydate\" value=\"".$this->currentDate."\">".$cellContent."</button></td>";
  108. }
  109. else
  110. {
  111. $day="\t\t<td class=\"current\"><button type=\"submit\" name=\"mydate\" value=\"".$this->currentDate."\">".$cellContent."</button></td>";
  112. }
  113. }
  114. else
  115. {
  116. $this->currentDate =null;
  117. $cellContent=null;
  118. $lastDay=date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->daysInMonth)));
  119. $this->currentWeek=date("W", strtotime($lastDay));
  120. $day="\t\t<td></td>";
  121. }
  122. // sunday? => last cell in the row stores week number:
  123. if ($cellNumber % 7 == 0 )
  124. {
  125. $day.="\n\t\t<td>". $this->currentWeek ."</td>";
  126. }
  127. $day.="\n";
  128. return $day;
  129. }
  130. // create navigation
  131. private function _createNavi()
  132. {
  133. // set locale here for labels displaying months in selected language
  134. setlocale(LC_TIME, 'en_EN.utf8');
  135. $nextMonth = $this->currentMonth==12?1:intval($this->currentMonth)+1;
  136. $nextYear = $this->currentMonth==12?intval($this->currentYear)+1:$this->currentYear;
  137. $preMonth = $this->currentMonth==1?12:intval($this->currentMonth)-1;
  138. $preYear = $this->currentMonth==1?intval($this->currentYear)-1:$this->currentYear;
  139. $dPrevMonth = date('Y-M-d',strtotime("{$preYear}-{$preMonth}-1"));
  140. $dThisMonth = date('Y-M-d',strtotime("{$this->currentYear}-{$this->currentMonth}-1"));
  141. $dNextMonth = date('Y-M-d',strtotime("{$nextYear}-{$nextMonth}-1"));
  142. $lblPrevMonth = strftime('%B', strtotime($dPrevMonth));
  143. $lblThisMonth = strftime('%B %Y', strtotime($dThisMonth));
  144. $lblNextMonth = strftime('%B', strtotime($dNextMonth));
  145. $preMonth=sprintf('%02d', $preMonth);
  146. $nextMonth=sprintf("%02d", $nextMonth);
  147. return <<<EOT
  148. <table class="heading">
  149. <tr>
  150. <td style="text-align: left">
  151. <a href="{$this->naviHref}?month={$preMonth}&year={$preYear}"> &lsaquo;&lsaquo; {$lblPrevMonth}</a>
  152. </td>
  153. <td style="text-align: center">{$lblThisMonth}</td>
  154. <td style="text-align: right">
  155. <a href="{$this->naviHref}?month={$nextMonth}&year={$nextYear}">{$lblNextMonth} &rsaquo;&rsaquo; </a>
  156. </td>
  157. <tr>
  158. </table>
  159. EOT;
  160. }
  161. // create calendar week labels
  162. private function _createLabels()
  163. {
  164. $content="</tr>\n\t";
  165. foreach($this->dayLabels as $index=>$label)
  166. {
  167. $content.='<td>'.$label.'</td>';
  168. }
  169. $content.='<td>W</td>';
  170. $content.="</tr>\n";
  171. return $content;
  172. }
  173. // calculate number of weeks in a particular month
  174. private function _weeksInMonth($month=null,$year=null)
  175. {
  176. if( null==($year) )
  177. {
  178. $year = date("Y",time());
  179. }
  180. if(null==($month))
  181. {
  182. $month = date("m",time());
  183. }
  184. // find number of days in this month
  185. $daysInMonths = $this->_daysInMonth($month,$year);
  186. $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7);
  187. $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths));
  188. $monthStartDay = date('N',strtotime($year.'-'.$month.'-01'));
  189. if($monthEndingDay<$monthStartDay)
  190. {
  191. $numOfweeks++;
  192. }
  193. return $numOfweeks;
  194. }
  195. // calculate number of days in a particular month
  196. private function _daysInMonth($month=null,$year=null)
  197. {
  198. if(null==($year))
  199. $year = date("Y",time());
  200. if(null==($month))
  201. $month = date("m",time());
  202. return date('t',strtotime($year.'-'.$month.'-01'));
  203. }
  204. }
  205. ?>