challenge 年間カレンダー

nを入力としてn年の年間カレンダーを返すプログラムを作ってください
少なくとも日曜日と土曜日が判別出来るようにしてください
出力は標準出力でもファイルでも構いません
デザインは各自のお好みで

出力例1:
(y-calendar 2008)=>
#=Saturday, @=Sunday
2008/1 1 2 3 4 #5 @6 7 ...
2008/2 1 #2 @3 4 5 6 7 ...
...
2008/12 1 2 3 4 5 #6 @7 ...

出力例2:
(y-calendar 2008)=>
        M T W T F S S M
2008/ 1   1 2 3 4 5 6 7 ...
2008/ 2         1 2 3 4 ...
...
2008/12 1 2 3 4 5 6 7 8 ...

出力例3:
(y-calendar 2008)は2008.htmlを出力する
2008.htmlの中身
----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>2008 calendar</title>
<style type="text/css">
* {font-family: monospace;}
span {margin: 0px 3px;}
span.sunday {color:red;font-weight:bold;}
span.saturday {color:blue;font-weight:bold;}
dd ul li{display:inline;}
</style>
</head>
<body>
<h1>2008 calendar</h1>
<dl>
<dt>2008/1</dt>
<dd><ul>
<li><span class="weekday">1</span></li>
<li><span class="weekday">2</span></li>
<li><span class="weekday">3</span></li>
<li><span class="weekday">4</span></li>
<li><span class="saturday">5</span></li>
<li><span class="sunday">6</span></li>
...
</ul></dd>
...
</dl>
</body>
</html>
----

Posted feedbacks - PHP

出力例3をテーブルに変えて。
テーブル用に空白を&nbsp;で埋めています。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
$year = 2008;
$weekdays = array( "S", "M", "T", "W", "T", "F", "S" );

for ( $month = 1; $month <= 12; $month++ ) {
    $month_ts = mktime( 0, 0, 0, $month, 1, $year );
    $days = date( 't', $month_ts );
    $weekday = date( 'w', $month_ts );
    $week = 0;
    for ( $day = 1; $day <= $days; $day++ ) {
        if ( $day == 1 && $weekday > 0 ) {
            for ( $i = 0; $i < $weekday; $i++ ) {
                $calendar[$month][$week][$i] = "&nbsp;";
            }
        }
        $calendar[$month][$week][] = $day;
        $weekday++;
        if ( $day == $days && $weekday < 7 ) {
            for ( $j = $weekday; $j < 7; $j++ ) {
                $calendar[$month][$week][$j] = "&nbsp;";
            }
        }
        if ( $weekday == 7 ) {
            $week++;
            $weekday = 0;
        }
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php echo($year); ?> calendar</title>
<style type="text/css">
* {font-family: monospace;}
td {text-align: right;}
.sunday {color:red;font-weight:bold;}
.saturday {color:blue;font-weight:bold;}
</style>
</head>
<body>
<h1><?php echo($year); ?> calendar</h1>
<dl>
<?php foreach ( $calendar as $month => $weeks ) { ?>
<dt><?php echo($year); ?>/<?php echo($month); ?></dt>
<dd>
<table>
<tr>
<?php
  foreach ( $weekdays as $key => $val ) {
    $class = "weekday";
    $class = ( $key === 0 )? "sunday": $class;
    $class = ( $key == 6 )? "saturday": $class;
?>
<th class="<?php echo($class); ?>"><?php echo($val); ?></th>
<?php } ?>
</tr>
<?php foreach ( $weeks as $week ) { ?>
<tr>
<?php
  foreach ( $week as $weekday => $day ) {
    $class = "weekday";
    $class = ( $weekday === 0 )? "sunday": $class;
    $class = ( $weekday == 6 )? "saturday": $class;
?>
<td class="<?php echo($class); ?>"><?php echo($day); ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
</dd>
<?php } ?>
</dl>
</body>
</html>

Index

Feed

Other

Link

Pathtraq

loading...