PHP 学習永久カレンダーの作成

*文
リリース: 2023-03-18 11:36:02
オリジナル
2266 人が閲覧しました

多くの PHP 初心者は、初めて学習するとき、PHP で何ができるのか、またその方法がわからず混乱するかもしれません。この記事では、誰もが PHP の使用方法を学べるように、PHP を使用して永久カレンダー関数を作成します。

PHP を使用して永久カレンダー機能を実装する際の重要なポイント:

処理対象の当月の合計日数を取得 $days

処理対象の当月の週の最初の曜日を取得 $dayofweek

$daysの役割: 何を処理するのかがわかる ループで月の日数を出力できる

$dayofweekの役割: 毎月1日の曜日がわかるだけでわかる日数を出力する前にスペース(空白)の数を出力する必要があります

「永久カレンダークラス」コードは次のとおりです:

<?php
/**
 * PHP万年历
 * @author Fly 2012/10/16
 */
class Calendar{
    protected $_table;//table表格
    protected $_currentDate;//当前日期
    protected $_year;    //年
    protected $_month;    //月
    protected $_days;    //给定的月份应有的天数
    protected $_dayofweek;//给定月份的 1号 是星期几
    /**
     * 构造函数
     */
    public function __construct() 
    {
        $this->_table="";
        $this->_year  = isset($_GET["y"])?$_GET["y"]:date("Y");
        $this->_month = isset($_GET["m"])?$_GET["m"]:date("m");
        if ($this->_month>12){//处理出现月份大于12的情况
            $this->_month=1;
            $this->_year++;
        }
        if ($this->_month<1){//处理出现月份小于1的情况
            $this->_month=12;
            $this->_year--;
        }
        $this->_currentDate = $this->_year.&#39;年&#39;.$this->_month.&#39;月份&#39;;//当前得到的日期信息
        $this->_days           = date("t",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份应有的天数
        $this->_dayofweek    = date("w",mktime(0,0,0,$this->_month,1,$this->_year));//得到给定的月份的 1号 是星期几
    }
    /**
     * 输出标题和表头信息
     */
    protected function _showTitle()
    {
        $this->_table="<table><thead><tr align=&#39;center&#39;><th colspan=&#39;7&#39;>".$this->_currentDate."</th></tr></thead>";
        $this->_table.="<tbody><tr>";
        $this->_table .="<td style=&#39;color:red&#39;>星期日</td>";
        $this->_table .="<td>星期一</td>";
        $this->_table .="<td>星期二</td>";
        $this->_table .="<td>星期三</td>";
        $this->_table .="<td>星期四</td>";
        $this->_table .="<td>星期五</td>";
        $this->_table .="<td style=&#39;color:red&#39;>星期六</td>";
        $this->_table.="</tr>";
    }
    /**
     * 输出日期信息
     * 根据当前日期输出日期信息
     */
    protected function _showDate()
    {
        $nums=$this->_dayofweek+1;
        for ($i=1;$i<=$this->_dayofweek;$i++){//输出1号之前的空白日期
            $this->_table.="<td> </td>";
        }
        for ($i=1;$i<=$this->_days;$i++){//输出天数信息
            if ($nums%7==0){//换行处理:7个一行
                $this->_table.="<td>$i</td></tr><tr>";    
            }else{
                $this->_table.="<td>$i</td>";
            }
            $nums++;
        }
        $this->_table.="</tbody></table>";
        $this->_table.="<h3><a href=&#39;?y=".($this->_year)."&m=".($this->_month-1)."&#39;>上一月</a>   ";
        $this->_table.="<a href=&#39;?y=".($this->_year)."&m=".($this->_month+1)."&#39;>下一月</a></h3>";
    }
    /**
     * 输出日历
     */
    public function showCalendar()
    {
        $this->_showTitle();
        $this->_showDate();
        echo $this->_table;
    }
}
$calc=new Calendar();
$calc->showCalendar();
ログイン後にコピー

関連する推奨事項:

PHP学習CURLクローラーの例

私の PHP 学習パス、PHP マスターの学習洞察

PHP 学習方法と経験

以上がPHP 学習永久カレンダーの作成の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!