LoginSignup
7
10

More than 5 years have passed since last update.

PHPでカレンダー作ってみた(1)

Last updated at Posted at 2018-05-05

1.td要素の作成

まずはDatePeriodを使って特定の日付オブジェクトを作成していきます

そもそもDatePeriodって何ぞや?って事で調べると「特定の期間の日付オブジェクトを作成する物」らしい。

要するにDatePeriodを使って自分で何日〜何日までの日付を作るよって事ができるらしいやってみる。

index.php
<?php

$body = '';
#DatePeriod:特定の期間の日付オブジェクトを作成
$period = new DatePeriod(
    #その月の月始まりだよと宣言
  new DateTime('first day of this month'),
  #1日毎の日付データを取得
  new DateInterval('P1D'),
  #次の月までの日付を取得(次の月の表示はされない)
  new DateTime('first day of next month')
);
foreach ($period as $day) {
  $body .= sprintf('<td>%d</td>', $day->format('d'));
}
 ?>

1.まず$period = new DatePeriod();の部分で
new DatePeriodで日付オブジェクトを作成しますよと定義。

2.定義したDatePeriodの処理内容を命令していく。
・new DateTime('first day of this month') - この部分でその月の初めから始まるよと宣言。
・new DateInterval('P1D') - 1日毎にデータ取ってきてねとお願いする
ちなみに('P7D')だと7日ごとにとってくる

7日毎だとこんな感じのイメージ
2018-05-01
2018-05-08
2018-05-15
2018-05-22
2018-05-29

・new DateTime('first day of next month') - これで初めの月から月末までの日付だよと宣言

3.foreachで$periodをループ

foreach ($period as $day) {
  $body .= sprintf('<td>%d</td>', $day->format('d'));
}

sprintfは書式付きで文字列を作ってくれるメソッドらしいのだけど正直良くわからん。
$bodyに連結した%dの値と'd'の値を代入している?

「%d」は10進数を表しているみたいなので、0~9の数字を'd'にフォーマットした$dayの数値と連結して1~31の数字を表示している??

何言ってるか分かんないので、その辺勉強しておきます。

これで1〜31日までのデータは取得できた。

index.php

<?php

$body = '';
#DatePeriod:特定の期間の日付オブジェクトを作成
$period = new DatePeriod(
#その月の月始まりだよと宣言
  new DateTime('first day of this month'),
#1日毎の日付データを取得
  new DateInterval('P1D'),
#次の月までの日付を取得(次の月の表示はされない)
  new DateTime('first day of next month')
);
foreach ($period as $day) {
  $body .= sprintf('<td>%d</td>', $day->format('d'));
}
 ?>

<!DOCTYPE html>
 <html lang="ja" dir="ltr">
   <head>
     <meta charset="utf-8">
     <title>Calendar</title>
     <link rel="stylesheet" href="styles.css">
   </head>
   <body>
     <table>
       <thead>
         <tr>
           <th><a href="#">&laquo;</a> </th>
           <th colspan="5"> August 2015</th>
           <th><a href="#">&laquo;</a> </th>
         </tr>
       </thead>
         <tbody>
           <tr>
             <td>Sun</td>
             <td>Mon</td>
             <td>Tue</td>
             <td>Web</td>
             <td>Thu</td>
             <td>Fri</td>
             <td>Sat</td>
           </tr>
           <tr>
             <?php echo $body; ?>
           </tr>
         </tbody>
         <tfoot>
           <th colspan="7"><a href="#">Today</a> </th>
         </tfoot>
     </table>
   </body>
 </html>

まとめ

とりあえずnew DatePeriod()を使えば日付オブジェクトが作成できる事と

foreach ($period as $day) {
  $body .= sprintf('<td>%d</td>', $day->format('d'));
}

こいつを使えば上手い事日付を取得できる事は分かった!!

参考

%dとかのお話はこちら参考
http://sandbox-silver.blogspot.jp/2013/08/phpd-s.html

        =============================続=============================

PHPでカレンダー作ってみた(2)

format('d')の補足
・DateTimeoブジェクトを好きな書式で表示するものらしい

公式サイトにdは01~31までを返すよって書いてますね

http://jp2.php.net/manual/ja/function.date.php
スクリーンショット 2018-05-04 17.49.02.png

週ごとに行変えを行う

index.php追加
foreach ($period as $day) {
#wで日曜日が0月曜日が1となるので、それを7で割った時の余りが0のときに行変えをしていくs
  if ($day->format('w') % 7 === 0) { $body .= '</tr><tr>';}
  $body .= sprintf('<td>%d</td>', $day->format('w'), $day->format('d'));
}

$day->format('w') % 7=== 0を使う事で日曜日(0)〜土曜日(6)までの7曜日毎のフォーマットになるので、それを7で割って0の時に行替を行うようにする。

{ $body .= '</tr><tr>';}

bodyとtrを連結することにより、テーブル形式で作成される

スクリーンショット 2018-05-04 18.10.37.png

とりあえずカレンダーの形にはなった。

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10