LoginSignup
0
0

More than 1 year has passed since last update.

過去の自分のコードを修正してみる - PHP カレンダー編③

Posted at

前回までのまとめ

これを


<?php
if ($t == 0) {
  print "<table border='0' width='100%' align='center' class='calender_gyou'>\n<tr>\n<td>\n<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
} elseif (($t == 1) or ($t == 2)) {
  print "<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
} else {
  print "<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t = 0; //次の段へ
}

こうした


if ($t === 0) :
?>
  <table class="calender_gyou first-table">
    <tr>
      <td>
<?php
endif;
?>
  <div class="<?php echo $t === 0 ? 'div-of-first-table' : 'div-of-month' ?>">
<?php
// 段の最後まで印字したら$tを0に戻して次の段にする…んだけど、
// 三項演算子で書いてみたら思ったより伝わらなさそうだったので普通にif文にする
// $t = $t > 2 ?  0 : $t++;
if ($t > 2) {
  $t = 0;
} else {
  $t++;
}
?>


.first-table {
  border-style:  none;
  width:         100%;
  text-align:    center; 
}
.div-of-first-table {
   float:        left;
   margin-right: 20px;
   margin-bottom:8px;
}
.div-of-month {
   float:        right;
   margin-right: 20px;
   margin-bottom:8px;
}

観測範囲を上に伸ばす


// テーブルの数を数えるための変数(改行処理に使う)
$t = 0;
?>
<div class="calender_div">
<?php
//月に1を足しながら12月まで繰り返し
for ($gatsu = 1; $gatsu < 13; $gatsu++) {
  // "日"は 1 から始める
  $hi = 1;
  // $gatsu 月1日の曜日を算出する
  $you = date("w", mktime(0, 0, 0, $gatsu, $hi, $nen));

……んー
曜日の求め方が気に入らない
というかDateTimeImmutableを知ってから、基本それしか使ってないからな…
あと見てわかるコメント多くない?当時は分からなかったから残してたのかな…

今の趣味に合わせて書き換える


// 4×3段で組みたいので今いくつか数える
$t = 0;
?>
<div class="calender_div">
<?php
for ($gatsu = 1; $gatsu < 13; $gatsu++) {
  $hi = 1;
  // カレンダーを何曜日から印字させるか求めたい
  // あ、今更ですが$nenはもっと上で定義してます
  $you = (new DateTimeImmutable("${nen}-${gatsu}-${hi}"))->format('w');

  // ここから前回までに修正したコード(cssは変わってないので省略)
  if ($t === 0) :
  ?>
    <table class="calender_gyou first-table">
      <tr>
        <td>
  <?php
  endif;
  ?>
  <div class="<?php echo $t === 0 ? 'div-of-first-table' : 'div-of-month' ?>">
  <?php
  // 段の最後まで印字したら$tを0に戻して次の段にする…んだけど、
  // 三項演算子で書いてみたら思ったより伝わらなさそうだったので普通にif文にする
  // $t = $t > 2 ?  0 : $t++;
  if ($t > 2) {
    $t = 0;
  } else {
    $t++;
  }

…あれ
これ変数tってなんとかならん?
gatsuから計算すればいいじゃん。tにインクリメントしたり初期化したりしなくていい気がする

tを計算で求めてみる


<div class="calender_div">
<?php
date_default_timezone_set('Asia/Tokyo');
for ($gatsu = 1; $gatsu < 13; $gatsu++) {
  // 今列のなかで何番目か。0ならその列の最後の月。[1, 2, 3, 0]になってく感じ
  // ここ見やすさ重視で値調整したほうがいいのかなぁ
  $t = $gatsu % 4;
  $hi = 1;
  // カレンダーを何曜日から印字させるか求めたい
  // あ、今更ですが$nenはもっと上で定義してます
  $you = (new DateTimeImmutable("${nen}-${gatsu}-${hi}"))->format('w');

  // $tの値が連動して変わるためその辺は修正
  if ($t === 1) :
  ?>
    <table class="calender_gyou first-table">
      <tr>
        <td>
  <?php
  endif;
  ?>
  <div class="<?php echo $t === 1 ? 'div-of-first-table' : 'div-of-month' ?>">

動かしてないけどこういうことだと思うんだ。
余力があるときにきちんと動かして修正かけるかもしれません。

0
0
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
0
0