[PHP] php 日历类 日历控件 →→→→→进入此内容的聊天室

来自 , 2021-01-01, 写在 PHP, 查看 190 次.
URL http://www.code666.cn/view/639d79cc
  1. <?php
  2. class CalendarForm {
  3.  
  4.   protected $year;
  5.   protected $month;
  6.   protected $day;
  7.   protected $weekend;
  8.   protected $currentdate;
  9.   protected $dayofmonth;
  10.   protected $day_count;
  11.   protected $num;
  12.   protected $week = array();
  13.   protected $retunrhtml = "";
  14.  
  15.   function __construct($year, $month) {
  16.     $this->makeWeeks($year, $month);
  17.   }
  18.  
  19.   public function setYearMonth($year, $month) {
  20.     $this->year = $year;
  21.     $this->month = $month;
  22.   }
  23.  
  24.   private function resetDayCount() {
  25.     $this->day_count = 1;
  26.   }
  27.  
  28.   private function setFirstWeek() {
  29.     $this->num = 0;
  30.   }
  31.  
  32.   public function getDayOfMonth($year, $month) {
  33.     $this->resetDayCount();
  34.     return date('t', mktime(0, 0, 0, $month, $this->day_count, $year));
  35.   }
  36.  
  37.   private function setDayOfMonth($year, $month) {
  38.     $this->dayofmonth = $this->getDayOfMonth($year, $month);
  39.   }
  40.  
  41.   private function getDayOfWeek() {
  42.     return date('w', mktime(0, 0, 0, $this->month, $this->day_count, $this->year));
  43.   }
  44.  
  45.   public function getNextMonth() {
  46.     return date('m', mktime(0, 0, 0, $this->month, 28, $this->year) + 432000);
  47.   }
  48.  
  49.   public function getNextYear() {
  50.     return date('Y', mktime(0, 0, 0, $this->month, 28, $this->year) + 432000);
  51.   }
  52.  
  53.   public function getPrevMonth() {
  54.     return date('m', mktime(0, 0, 0, $this->month, 1, $this->year) - 432000);
  55.   }
  56.  
  57.   public function getPrevYear() {
  58.     return date('Y', mktime(0, 0, 0, $this->month, 1, $this->year) - 432000);
  59.   }
  60.  
  61.   private function makeWeeks($year, $month) {
  62.  
  63.     $this->setYearMonth($year, $month);
  64.     $this->setDayOfMonth($this->year, $this->month);
  65.     $this->setFirstWeek();
  66.  
  67.     $this->num = 0;
  68.     for ($i = 0; $i < 7; $i++) {
  69.       $dayofweek = $this->getDayOfWeek();
  70.       $dayofweek = $dayofweek - 1;
  71.       if ($dayofweek == -1) {
  72.         $dayofweek = 6;
  73.       }
  74.       if ($dayofweek == $i) {
  75.         $this->week[$this->num][$i] = $this->day_count;
  76.         $this->day_count++;
  77.       }
  78.       else {
  79.         $this->week[$this->num][$i] = "";
  80.       }
  81.     }
  82.     while (TRUE) {
  83.       $this->num++;
  84.       for ($i = 0; $i < 7; $i++) {
  85.         $this->week[$this->num][$i] = $this->day_count;
  86.         $this->day_count++;
  87.         if ($this->day_count > $this->dayofmonth) {
  88.           break;
  89.         }
  90.       }
  91.       if ($this->day_count > $this->dayofmonth) {
  92.         break;
  93.       }
  94.     }
  95.  
  96.   }
  97.  
  98.   public function getCalendarHeader() {
  99.     $this->retunrhtml =
  100.       "<table class=\\"calendar-table\\">" .
  101.       "<tbody>" .
  102.       "<tr><th colspan=\\"7\\">" . $this->month . "/" . $this->year . "</th></tr>" .
  103.       "<tr>" .
  104.       "<th style=\\"text-align: center;\\">Monday</th>" .
  105.       "<th style=\\"text-align: center;\\">Tuesday</th>" .
  106.       "<th style=\\"text-align: center;\\">Wednesday</th>" .
  107.       "<th style=\\"text-align: center;\\">Thursday</th>" .
  108.       "<th style=\\"text-align: center;\\">Friday</th>" .
  109.       "<th style=\\"text-align: center;\\">Saturday</th>" .
  110.       "<th style=\\"text-align: center;\\">Sunday</th>" .
  111.       "</tr>";
  112.   }
  113.  
  114.   public function getCalendarFooter() {
  115.     $this->retunrhtml .= "</tbody></table>";
  116.   }
  117.  
  118.   public function getBeginTR() {
  119.     $this->retunrhtml .= "<tr>";
  120.   }
  121.  
  122.   public function getEndTR() {
  123.     $this->retunrhtml .= "</tr>";
  124.   }
  125.  
  126.   protected function getDay() {
  127.     return $this->day;
  128.   }
  129.  
  130.   protected function getMonth() {
  131.     return $this->month;
  132.   }
  133.  
  134.   protected function getYear() {
  135.     return $this->year;
  136.   }
  137.  
  138.   protected function isWeekend() {
  139.     return $this->weekend;
  140.   }
  141.  
  142.   protected function isCurrent() {
  143.     return $this->currentdate;
  144.   }
  145.  
  146.   public function getTDHref() {
  147.     return $this->getDay();
  148.   }
  149.  
  150.   public function getTD() {
  151.     $class = '';
  152.     $td = "td";
  153.     if ($this->isCurrent()) {
  154.       $class = 'today';
  155.     }
  156.     $this->retunrhtml .= "<$td class=\\"$class\\">" . $this->getTDHref() . "</$td>";
  157.   }
  158.  
  159.   public function getTDWeekend() {
  160.     $class = '';
  161.     $td = "td";
  162.     if ($this->isCurrent()) {
  163.       $class = 'today';
  164.     }
  165.     $this->retunrhtml .= "<$td class=\\"$class\\">" . $this->getTDHref() . "</$td>";
  166.   }
  167.  
  168.   protected function makeCodeMonth($year, $month) {
  169.     $this->makeWeeks($year, $month);
  170.     $this->getCalendarHeader();
  171.     for ($i = 0; $i < count($this->week); $i++) {
  172.       $this->getBeginTR();
  173.       for ($j = 0; $j < 7; $j++) {
  174.  
  175.         if (!empty($this->week[$i][$j])) {
  176.           $this->day = $this->week[$i][$j];
  177.           $this->currentdate = 0;
  178.           if ($this->year == date('Y') && $this->month == date('m') && $this->day == date('j')) {
  179.             $this->currentdate = 1;
  180.           }
  181.           if ($j == 5 || $j == 6) {
  182.             $this->weekend = 1;
  183.             $this->getTDWeekend();
  184.           }
  185.           else {
  186.             $this->weekend = 0;
  187.             $this->getTD();
  188.           }
  189.  
  190.         }
  191.         else {
  192.           $this->retunrhtml .= "<td> </td>";
  193.         }
  194.  
  195.       }
  196.       $this->getEndTR();
  197.     }
  198.     $this->getCalendarFooter();
  199.   }
  200.  
  201.   public function getCodeMonth() {
  202.     $this->makeCodeMonth($this->year, $this->month);
  203.     return $this->retunrhtml;
  204.   }
  205.  
  206.   public function showCodeMonth() {
  207.     echo $this->getCodeMonth();
  208.   }
  209.  
  210. }
  211.  
  212. class TechCalendarForm extends CalendarForm {
  213.   public function getTDHref() {
  214.     if ($this->isWeekend()) {
  215.       $font = "<font color=\\"#FF3F4F\\">";
  216.    }
  217.     else {
  218.       $font = "<font color=\\"#4A5B6C\\">";
  219.    }
  220.     return "<a href=\\"" . $_SERVER["PHP_SELF"] . "?action=showdate&date=" . parent::getYear() . "-" . parent::getMonth() . "-" . parent::getDay() . "\\">" . $font . parent::getDay() . "</font></a>";
  221.   }
  222. }
  223.  
  224.  

回复 "php 日历类 日历控件"

这儿你可以回复上面这条便签

captcha