123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <html>
- <head>
- <link href="calendar.css?ver=123" type="text/css" rel="stylesheet" />
- </head>
- <body>
- <?php
- echo '<p align="center">';
- $mydate = date('Y-m-d');
- if(isset($_POST['mydate']))
- {
- $mydate = $_POST['mydate'];
- }
- echo "Date: $mydate";
- echo "</p>\n";
- $calendar = new Calendar($mydate);
- echo $calendar->show();
- ?>
- </body>
- </html>
- <!-- class Calendar -->
- <?php
- date_default_timezone_set('Europe/Vienna');
- class Calendar
- {
- // Constructor
- public function __construct($selectedDate)
- {
- $this->selectedDate=$selectedDate;
- $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
- }
- // Properties
- private $dayLabels = array("MO","TU","WE","TH","FR","SA","SU");
- private $currentYear=0;
- private $currentMonth=0;
- private $currentDay=0;
- private $currentDate=null;
- public $selectedDate;
- private $currentWeek=0;
- private $daysInMonth=0;
- private $naviHref= null;
- // print out the calendar
- public function show()
- {
- $year = null;
- $month = null;
- if(null==$year&&isset($_GET['year']))
- {
- $year = $_GET['year'];
- }
- else if(null==$year)
- {
- $year = date("Y",time());
- }
- if(null==$month&&isset($_GET['month']))
- {
- $month = $_GET['month'];
- }
- else if(null==$month)
- {
- $month = date("m",time());
- }
- $this->currentYear=$year;
- $this->currentMonth=$month;
- $this->daysInMonth=$this->_daysInMonth($month,$year);
- $weeksInMonth = $this->_weeksInMonth($month,$year);
- $content="\n<div class=\"calendar\">\n".
- $this->_createNavi().
- "<table class=\"calendar\">\n".
- "<form method=\"post\" action=\"\">\n".
- $this->_createLabels();
- // Create weeks in a month
- for( $i=0; $i<$weeksInMonth; $i++ )
- {
- $content.="\t<tr>\n";
- //Create days in a week
- for($j=1;$j<=7;$j++)
- {
- $content.=$this->_showDay($i*7+$j);
- }
- $content.="\t</tr>\n";
- }
- $content.="</form>\n";
- $content.="</table>\n";
- $content.="</div>\n";
- return $content;
- }
- // get single of a day
- private function _showDay($cellNumber)
- {
- $day="";
- if($this->currentDay==0)
- {
- $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));
- if(intval($cellNumber) == intval($firstDayOfTheWeek))
- {
- $this->currentDay=1;
- }
- }
- if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) )
- {
- $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));
- $cellContent = $this->currentDay;
- $this->currentDay++;
- $this->currentWeek=date("W", strtotime($this->currentDate));
- if ($this->currentDate != $this->selectedDate)
- {
- $day="\t\t<td><button type=\"submit\" name=\"mydate\" value=\"".$this->currentDate."\">".$cellContent."</button></td>";
- }
- else
- {
- $day="\t\t<td class=\"current\"><button type=\"submit\" name=\"mydate\" value=\"".$this->currentDate."\">".$cellContent."</button></td>";
- }
- }
- else
- {
- $this->currentDate =null;
- $cellContent=null;
- $lastDay=date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->daysInMonth)));
- $this->currentWeek=date("W", strtotime($lastDay));
- $day="\t\t<td></td>";
- }
- // sunday? => last cell in the row stores week number:
- if ($cellNumber % 7 == 0 )
- {
- $day.="\n\t\t<td>". $this->currentWeek ."</td>";
- }
- $day.="\n";
- return $day;
- }
- // create navigation
- private function _createNavi()
- {
- // set locale here for labels displaying months in selected language
- setlocale(LC_TIME, 'en_EN.utf8');
- $nextMonth = $this->currentMonth==12?1:intval($this->currentMonth)+1;
- $nextYear = $this->currentMonth==12?intval($this->currentYear)+1:$this->currentYear;
- $preMonth = $this->currentMonth==1?12:intval($this->currentMonth)-1;
- $preYear = $this->currentMonth==1?intval($this->currentYear)-1:$this->currentYear;
- $dPrevMonth = date('Y-M-d',strtotime("{$preYear}-{$preMonth}-1"));
- $dThisMonth = date('Y-M-d',strtotime("{$this->currentYear}-{$this->currentMonth}-1"));
- $dNextMonth = date('Y-M-d',strtotime("{$nextYear}-{$nextMonth}-1"));
- $lblPrevMonth = strftime('%B', strtotime($dPrevMonth));
- $lblThisMonth = strftime('%B %Y', strtotime($dThisMonth));
- $lblNextMonth = strftime('%B', strtotime($dNextMonth));
- $preMonth=sprintf('%02d', $preMonth);
- $nextMonth=sprintf("%02d", $nextMonth);
- return <<<EOT
- <table class="heading">
- <tr>
- <td style="text-align: left">
- <a href="{$this->naviHref}?month={$preMonth}&year={$preYear}"> ‹‹ {$lblPrevMonth}</a>
- </td>
- <td style="text-align: center">{$lblThisMonth}</td>
- <td style="text-align: right">
- <a href="{$this->naviHref}?month={$nextMonth}&year={$nextYear}">{$lblNextMonth} ›› </a>
- </td>
- <tr>
- </table>
- EOT;
- }
- // create calendar week labels
- private function _createLabels()
- {
- $content="</tr>\n\t";
- foreach($this->dayLabels as $index=>$label)
- {
- $content.='<td>'.$label.'</td>';
- }
- $content.='<td>W</td>';
- $content.="</tr>\n";
- return $content;
- }
- // calculate number of weeks in a particular month
- private function _weeksInMonth($month=null,$year=null)
- {
- if( null==($year) )
- {
- $year = date("Y",time());
- }
- if(null==($month))
- {
- $month = date("m",time());
- }
- // find number of days in this month
- $daysInMonths = $this->_daysInMonth($month,$year);
- $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7);
- $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths));
- $monthStartDay = date('N',strtotime($year.'-'.$month.'-01'));
- if($monthEndingDay<$monthStartDay)
- {
- $numOfweeks++;
- }
- return $numOfweeks;
- }
- // calculate number of days in a particular month
- private function _daysInMonth($month=null,$year=null)
- {
- if(null==($year))
- $year = date("Y",time());
- if(null==($month))
- $month = date("m",time());
- return date('t',strtotime($year.'-'.$month.'-01'));
- }
- }
- ?>
|