カレンダー付箋ボード

カレンダーと付箋ボードのWindowsアプリ完成!

詳細はこちら

【初心者】PHPでシンプルカレンダーを作成してみる。

PHPの勉強をはじめました。なかなか難しいですが、まだ挫折はしていないぞ。まずはシンプルなカレンダーを作成してみます。

この本で勉強

まずは本を買って勉強。
カラーで見やすいけど、実践例が少ないかな。
別の本をもう1冊買おうか?とも思いましたが、とりあえず簡単なものを作ってみます。

シンプルカレンダー

シンプルカレンダーがよさそうです。
基本的な文法も使うし、HTML、CSS、PHPを一通り使いながら作成できます。
Bootstrapなんて聞いたことない(本にも載ってない)のも使うようですが、できるかな?

コード

いろんなサイトをみて作ってみました。

  1. <?php
  2. //タイムゾーン設定
  3. date_default_timezone_set('Asia/Tokyo');
  4. //前月・次月選択されたGET年月を取得
  5. if(isset($_GET['ym'])){
  6.     $ym = $_GET['ym'];
  7. }else{
  8.     //今月の年月
  9.     $ym = date('Y-m');
  10. }
  11. //strtotime('Y-m-01')
  12. $timestamp = strtotime($ym . '-01');
  13. if($timestamp === false){//形式チェックを追加
  14.     //falseが返ってきた時は、現在の年月を取得
  15.     $ym = date('Y-m');
  16.     $timestamp = strtotime($ym . '-01');
  17. }
  18. //今月の日付 例)2020-10-2
  19. $today = date('Y-m-j');
  20. //カレンダーのタイトル作成 例)2020年10月
  21. $html_title = date('Y年n月', $timestamp);//date
  22. //前月・次月の年月を取得
  23. //strtotime(,基準)
  24. $prev = date('Y-m', strtotime('-1 month', $timestamp));
  25. $next = date('Y-m', strtotime('+1 month', $timestamp));
  26. //該当月の日数取得
  27. $day_count = date('t', $timestamp);
  28. //1日が何曜日か
  29. $youbi = date('w', $timestamp);
  30. //カレンダー作成準備
  31. $mouths = [];
  32. $week = '';
  33. //第1週目:空のセルを追加
  34. //str_repeat(文字列, 反復回数)
  35. $week .= str_repeat('<td></td>', $youbi);
  36. for($day = 1; $day <= $day_count; $day++, $youbi++){
  37.     
  38.     $date = $ym . '-' . $day;
  39.     
  40.     if($today == $date){
  41.         
  42.         $week .= '<td class="today">' . $day;//今日の場合はclassにtoday
  43.     } else {
  44.         $week .= '<td>' . $day;
  45.     }
  46.     $week .= '</td>';
  47.     
  48.     if($youbi % 7 == 6 || $day == $day_count){//週終わり、月終わりの場合
  49.         //%は余りを求める、||はまたは
  50.         //土曜日を取得
  51.         
  52.         if($day == $day_count){//月の最終日、空セルを追加
  53.             $week .= str_repeat('<td></td>', 6 - ($youbi % 7));
  54.         }
  55.         
  56.         $mouths[] = '<tr>' . $week . '</tr>'; //mouths配列にtrと$weekを追加
  57.         
  58.         $week = '';//weekをリセット
  59.     }
  60. }
  61.     
  62. ?>
  63. <!DOCTYPE html>
  64. <html lang="ja">
  65. <head>
  66.     <meta charset="utf-8">
  67.     <title>PHPカレンダー</title>
  68.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
  69.     <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
  70.     <style>
  71.       .container {
  72.         font-family: 'Noto Sans', sans-serif; /*--GoogleFontsを使用--*/
  73.           margin-top: 80px;
  74.       }
  75.         h3 {
  76.             margin-bottom: 30px;
  77.         }
  78.         th {
  79.             height: 30px;
  80.             text-align: center;
  81.         }
  82.         td {
  83.             height: 100px;
  84.         }
  85.         .today {
  86.             background-color: #B7E8DF;/*日付が今日の場合は背景薄緑*/
  87.         }
  88.         th:nth-of-type(1), td:nth-of-type(1) {/*日曜日は赤*/
  89.             color: red;
  90.         }
  91.         th:nth-of-type(7), td:nth-of-type(7) {/*土曜日は青*/
  92.             color: blue;
  93.         }
  94.         h3 {
  95.             margin-top:30px;
  96.         }
  97.     </style>
  98. </head>
  99. <body>
  100.     <div class="container">
  101.         <h3><a href="?ym=<?php echo $prev; ?>">&lt;</a><?php echo $html_title; ?><a href="?ym=<?php echo $next; ?>">&gt;</a></h3>
  102.         <table class="table table-bordered">
  103.             <tr>
  104.                 <th>日</th>
  105.                 <th>月</th>
  106.                 <th>火</th>
  107.                 <th>水</th>
  108.                 <th>木</th>
  109.                 <th>金</th>
  110.                 <th>土</th>
  111.             </tr>
  112.              <?php
  113.                 foreach ($mouths as $week) {
  114.                     echo $week;
  115.                 }
  116.             ?>
  117.         </table>
  118.     </div>
  119. </body>
  120. </html>

こんな感じでHTML CSS PHPを1枚で書いています。
最初はこのほうがわかりやすいかも。

これを実行すると・・・

で、で、できましたー!
今日の日付のところはちゃんと色がついています。
「<」で先月が表示されますよ。

いろんなサイトをみて、ほぼコピぺしただけなので出来て当たり前ですが、うれしい。

コードの理解

コードを理解しなきゃ勉強にならないので、コードを読み解きます。

45行目からが嫌~な感じです。

45行目:1日の曜日の前部分を空白で埋めます。
47行目から:ループしながら、今日の日付を探してclass=”today”
59行目から:土曜日(週終わり)または月末の判定をします。
63行目から:月末の場合は後部分を空白で埋めます。

82行目:Bootstrapを使用します。(カレンダーの枠)
83行目:Googleフォントを使用します。
84~111行目:スタイル設定
115行目:<2024年1月>の表示
127行目:foreachで$mouthsから$weekを取り出して表示

まとめ

理解するのに時間がかかりました。
まだまだ勉強の日々です。
このシンプルカレンダーから祝日設定、月曜はじまり、任意の休み設定、スケジュール入力などをやっていきたいと思います。