youtubeの動画を参考にPHPでカレンダーを写経しているのですが、どこかに記述ミスがあるのか、翌月、前月が切り替わりません。
動画とにらめっこしているのですが、なかなか間違いを見つけられずにいるのでアドバイスいただけると助かります。
index.php
1<?php 2 $month = 0; 3 $year = 0; 4 if (isset($_GET['y'])) { 5 $year = intval($_GET['y']); 6 } 7 if (isset($_GET['m'])) { 8 $month = intval($_GET['m']); 9 } 10 if (!$year || !$month || !checkdate($month, 1, $year)) { 11 // 現在の年月 12 $now_date = new DateTime(); 13 $year = $now_date->format('Y'); 14 $month = $now_date->format('m'); 15 } 16 try { 17 $datetime = new Datetime("{$year}-{$month}-1"); // 表示する月 18 } catch (Exception $e) { 19 echo $e->getMessage(); 20 exit(1); 21 } 22?> 23<!DOCTYPE html> 24<html lang="ja"> 25<head> 26 <meta charset="UTF-8"> 27 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 28 <title>PHP カレンダー</title> 29</head> 30<body> 31 <?= $datetime->format('Y年m月') ?> 32 <p> 33 <?php 34 $datetime->modify('-1 month'); 35 ?> 36 <a href="calendar.php?y=<?= $datetime->format('Y') ?>&m<?= $datetime->format('m') ?>">前月へ</a> 37 <?php 38 $datetime->modify('+2 month'); 39 ?> 40 <a href="calendar.php?y=<?= $datetime->format('Y') ?>&m<?= $datetime->format('m') ?> ">翌月へ</a> 41 </p> 42</body> 43</html> 44
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/04 14:00
2019/04/04 14:01