簡單實用PHP行事曆程式代碼

來源:互聯網
上載者:User

PHP行事曆程式,功能都是福士化的,可以下拉切換年月,上一年下一月下一年上一月,太另類的沒去寫,主要的寫出來了,擴充起來就方便多了,標題為什麼要叫精美呢,是因自已感覺介面還過得去,哈哈,讓大家見笑了,不足之處還請指出。

效果代碼如下

php日曆核心代碼

 代碼如下 複製代碼

<?php
//日曆類
class calendar {
    //當前的年
    private $year;
    //當前的月
    private $month;
    //一個月中第一天是星期幾
    private $start_weekday;
    //當前月的天數
    private $days;
    //最大數與最小年數,最大與最小月數
    private $yearMonth = array(2080, 1900, 12, 1);
    //建構函式
    function __construct() {
        if (isset($_GET['year'])) {
           $this->year = $_GET['year'];
        }
            if (isset($_GET['month'])) {
            $this->month = $_GET['month'];
        }
        $this->pnYm($this->year, $this->month);
        $this->days = date('t', mktime(0, 0, 0, $this->month, 1, $this->year));
        $this->start_weekday = date('w', mktime(0, 0, 0, $this->month, 1, $this->year));
        $this->style();
    }
    //輸出
    private function style() {
        echo '<table id="calendar">';
        $this->weeklist();
        $this->daylist();
        echo '<table>';
    }
    //年月參數判斷
    private function ymCheck($year, $month) {
        if (!is_numeric($year)) {
            $year = date('Y');
        }
        if (!is_numeric($month)) {
            $month = date('m');
        }
        if ($month < $this->yearMonth[3]) {
            $month = $this->yearMonth[2];
            $year -= 1;
        }
        if ($month > $this->yearMonth[2]) {
            $month = $this->yearMonth[3];
            $year = intval($year) + 1;
        }
        $year = $year < $this->yearMonth[1] ? $this->yearMonth[1] : $year;
        $year = $year > $this->yearMonth[0] ? $this->yearMonth[0] : $year;
        return array($year, $month);
    }
    //上一年、下一年、上一月、下一月
    private function pnYm($year, $month) {
        $ym = $this->ymCheck($year, $month);
        $this->year = $ym[0];
        $this->month = $ym[1];
    }
    //weeklist周列表
    private function weeklist() {
        $week = array('日','一','二','三','四','五','六');
        echo '<tr>';
        foreach ($week as $val) {
            echo '<th>'.$val.'</th>';
        }
        echo '</tr>';
    }
    //daylist天列表
    private function daylist() {
        //年月日導航
        echo '<tr>';
        echo '<td><a title="上一年" href="?year='.($this->year-1).'&month='.$this->month.'"><<</a></td>';
        echo '<td><a title="上一月" href="?year='.$this->year.'&month='.($this->month-1).'"><</a></td>';
        echo '<td colspan="3">';
        echo '<form action="?" method="get" id="form">';
        echo '<select name="year" onchange="formaction()">';
        for ($i = $this->yearMonth[1]; $i <= $this->yearMonth[0]; $i++) {
             if ($i == $this->year) {
                   echo '<option value="'.$i.'" selected="selected">'.$i.'年</option>';
             }else {
                   echo '<option value="'.$i.'">'.$i.'年</option>';
             }
        }
        echo '</select>';
        echo '<select name="month" onchange="formaction()">';
        for ($i = $this->yearMonth[3]; $i <= $this->yearMonth[2]; $i++) {
            if ($i == $this->month) {
                 echo '<option value="'.$i.'" selected="selected">'.$i.'月</option>';
            }else {
                 echo '<option value="'.$i.'">'.$i.'月</option>';
            }
        }
       echo '</select></form></td>';
       echo '<td><a title="下一月" href="?year='.$this->year.'&month='.($this->month+1).'">></a></td>';
       echo '<td><a title="下一年" href="?year='.($this->year+1).'&month='.$this->month.'">>></a></td>';
       echo '</tr>';

       echo '<tr>';
       //輸出空格(當前一個月第一天前面要空出來的)
       for($i = 0; $i < $this->start_weekday; $i++) {
             echo '<td>&nbsp;</td>';
       }
       for ($k = 1; $k <= $this->days; $k++) {
            $i++;
            if ($k == date('d')) {
                  echo '<td>'.$k.'</td>';
            }else {
                  echo '<td>'.$k.'</td>';
            }
            if ($i % 7 == 0) {
                 if ($k != $this->days) {
                        echo '</tr><tr>';
                 }
            }
        }
         echo '</tr>';
    }
}
?>

html+css代碼

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP行事曆程式</title>
<style>
#calendar { width:auto; margin:0 auto; margin-top:100px; border:0; border-collapse:collapse; box-shadow:0px 0px 4px #ddd; font-size:12px; text-align:center; font-family:"微軟雅黑"; color:#333; border:solid 1px #c5e2ff; }
#calendar tr { width:auto; height:34px; line-height:34px; }
#calendar tr th { width:44px; background:#c5e2ff; }
#calendar tr td { background:#fff; }
#calendar tr td.tdbg { background:#c5e2ff; }
#calendar tr td:hover { background:#FFC; }
#calendar tr td a { text-decoration:none; color:#f50; font-weight:900; }
#calendar select { width:auto; border:solid 1px #c5c5c5; padding:2px 0 2px 0; background:#fff; float:left; margin-left:5px; }
</style>
<script>
function formaction() {
    var form = document.getElementById('form');
    form.submit();
}
</script>
</head>
<body>
<?php
require 'init.php';
$calendar = new calendar();
?>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.