-
-
Save codeforfun-jp/76cb2960b6462db9c41c0cf3cd5d5abc to your computer and use it in GitHub Desktop.
PHP Calendar 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// タイムゾーンを設定 | |
date_default_timezone_set('Asia/Tokyo'); | |
// 前月・次月リンクが押された場合は、GETパラメーターから年月を取得 | |
if (isset($_GET['ym'])) { | |
$ym = $_GET['ym']; | |
} else { | |
// 今月の年月を表示 | |
$ym = date('Y-m'); | |
} | |
// タイムスタンプを作成し、フォーマットをチェックする | |
$timestamp = strtotime($ym . '-01'); | |
if ($timestamp === false) { | |
$ym = date('Y-m'); | |
$timestamp = strtotime($ym . '-01'); | |
} | |
// 今日の日付 フォーマット 例)2021-06-3 | |
$today = date('Y-m-j'); | |
// カレンダーのタイトルを作成 例)2021年6月 | |
$html_title = date('Y年n月', $timestamp); | |
// 前月・次月の年月を取得 | |
// 方法1:mktimeを使う mktime(hour,minute,second,month,day,year) | |
$prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp))); | |
$next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp))); | |
// 方法2:strtotimeを使う | |
// $prev = date('Y-m', strtotime('-1 month', $timestamp)); | |
// $next = date('Y-m', strtotime('+1 month', $timestamp)); | |
// 該当月の日数を取得 | |
$day_count = date('t', $timestamp); | |
// 1日が何曜日か 0:日 1:月 2:火 ... 6:土 | |
// 方法1:mktimeを使う | |
$youbi = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp))); | |
// 方法2 | |
// $youbi = date('w', $timestamp); | |
// カレンダー作成の準備 | |
$weeks = []; | |
$week = ''; | |
// 第1週目:空のセルを追加 | |
// 例)1日が火曜日だった場合、日・月曜日の2つ分の空セルを追加する | |
$week .= str_repeat('<td></td>', $youbi); | |
for ( $day = 1; $day <= $day_count; $day++, $youbi++) { | |
// 2021-06-3 | |
$date = $ym . '-' . $day; | |
if ($today == $date) { | |
// 今日の日付の場合は、class="today"をつける | |
$week .= '<td class="today">' . $day; | |
} else { | |
$week .= '<td>' . $day; | |
} | |
$week .= '</td>'; | |
// 週終わり、または、月終わりの場合 | |
if ($youbi % 7 == 6 || $day == $day_count) { | |
if ($day == $day_count) { | |
// 月の最終日の場合、空セルを追加 | |
// 例)最終日が水曜日の場合、木・金・土曜日の空セルを追加 | |
$week .= str_repeat('<td></td>', 6 - $youbi % 7); | |
} | |
// weeks配列にtrと$weekを追加する | |
$weeks[] = '<tr>' . $week . '</tr>'; | |
// weekをリセット | |
$week = ''; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>PHPカレンダー</title> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> | |
<style> | |
a { | |
text-decoration: none; | |
} | |
th { | |
height: 30px; | |
text-align: center; | |
} | |
td { | |
height: 100px; | |
} | |
.today { | |
background: orange !important; | |
} | |
th:nth-of-type(1), td:nth-of-type(1) { | |
color: red; | |
} | |
th:nth-of-type(7), td:nth-of-type(7) { | |
color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container mt-5"> | |
<h3 class="mb-4"><a href="?ym=<?= $prev ?>"><</a><span class="mx-3"><?= $html_title ?></span><a href="?ym=<?= $next ?>">></a></h3> | |
<table class="table table-bordered"> | |
<tr> | |
<th>日</th> | |
<th>月</th> | |
<th>火</th> | |
<th>水</th> | |
<th>木</th> | |
<th>金</th> | |
<th>土</th> | |
</tr> | |
<?php | |
foreach ($weeks as $week) { | |
echo $week; | |
} | |
?> | |
</table> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment