php
1$tail = '';
なぜ空文字なのか?
php
1$lastDayOfPrevMonth = new DateTime('last day of ' . $yearMonth . ' -1 month');
引数が3つあるのですがこれはもう暗記なのでしょうか?慣れでしょうか?
公式サイトのどこに載っているのでしょうか?
ひとつひとう覚えるわけにはいかないと思います。
php
1 <th><a href="/?t=<?php echo h($next);?>">»</a></th>
/?t=<?phpこの部分がわからないです。tを渡すのになぜ/が入ってくるのか?
phpのタグはこれですので<?php
?>
/?t=の?がわからないです。
php
1<?php 2 3function h($s) { 4 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 5} 6 7try { 8 if (!isset($_GET['t']) || !preg_match('/\A\d{4}-\d{2}\z/', $_GET['t'])) { 9 throw new Exception(); 10 } 11 $thisMonth = new DateTime($_GET['t']); 12} catch (Exception $e) { 13 $thisMonth = new DateTime('first day of this month'); 14} 15 16$dt = clone $thisMonth; 17$prev = $dt->modify('-1 month')->format('Y-m'); 18$dt = clone $thisMonth; 19$next = $dt->modify('+1 month')->format('Y-m'); 20 21$yearMonth = $thisMonth->format('F Y'); 22 23$tail = ''; 24$lastDayOfPrevMonth = new DateTime('last day of ' . $yearMonth . ' -1 month'); 25while ($lastDayOfPrevMonth->format('w') < 6) { 26 $tail = sprintf('<td class="gray">%d</td>', $lastDayOfPrevMonth->format('d')) . $tail; 27 $lastDayOfPrevMonth->sub(new DateInterval('P1D')); 28} 29 30$body = ''; 31$period = new DatePeriod( 32 new DateTime('first day of ' . $yearMonth), 33 new DateInterval('P1D'), 34 new DateTime('first day of ' . $yearMonth . ' +1 month') 35); 36$today = new DateTime('today'); 37foreach ($period as $day) { 38 if ($day->format('w') % 7 === 0) { $body .= '</tr><tr>'; } 39 $todayClass = ($day->format('Y-m-d') === $today->format('Y-m-d')) ? 'today' : ''; 40 $body .= sprintf('<td class="youbi_%d %s">%d</td>', $day->format('w'), $todayClass, $day->format('d')); 41} 42 43$head = ''; 44$firstDayOfNextMonth = new DateTime('first day of ' . $yearMonth . ' +1 month'); 45while ($firstDayOfNextMonth->format('w') > 0) { 46 $head .= sprintf('<td class="gray">%d</td>', $firstDayOfNextMonth->format('d')); 47 $firstDayOfNextMonth->add(new DateInterval('P1D')); 48} 49 50$html = '<tr>' . $tail . $body . $head . '</tr>'; 51 52?> 53<!DOCTYPE html> 54<html lang="ja"> 55<head> 56 <meta charset="utf-8"> 57 <title>Calendar</title> 58 <link rel="stylesheet" href="styles.css"> 59</head> 60<body> 61 <table> 62 <thead> 63 <tr> 64 <th><a href="/?t=<?php echo h($prev); ?>">«</a></th> 65 <th colspan="5"><?php echo h($yearMonth); ?></th> 66 <th><a href="/?t=<?php echo h($next); ?>">»</a></th> 67 </tr> 68 </thead> 69 <tbody> 70 <tr> 71 <td>Sun</td> 72 <td>Mon</td> 73 <td>Tue</td> 74 <td>Wed</td> 75 <td>Thu</td> 76 <td>Fri</td> 77 <td>Sat</td> 78 </tr> 79 <?php echo $html; ?> 80 </tbody> 81 <tfoot> 82 <tr> 83 <th colspan="7"><a href="/">Today</a></th> 84 </tr> 85 </tfoot> 86 </table> 87</body> 88</html>
css
1body { 2 font-family: Arial, sans-serif; 3 font-size: 14px; 4} 5a { 6 text-decoration: none; 7} 8table { 9 margin: 15px auto; 10 border: 1px solid #ddd; 11 border-collapse: collapse; 12} 13th { 14 background: #eee; 15} 16th, td { 17 padding: 7px; 18 text-align: center; 19} 20 21.youbi_0 { 22 color: red; 23} 24.youbi_6 { 25 color: blue; 26} 27.today { 28 font-weight: bold; 29} 30.gray { 31 color: #dedede; 32}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。