dポイントプレゼントキャンペーン実施中!

今phpでカレンダーを作成しています。
その月の最終日以降に翌月の日付を表示させたいのですが、

if($i>$hiniti){
print("<td>w</td>");
}
だと、最終日の次の日(翌月の1日)しか表示させませんよね?

カレンダーの表を翌月の最初のほうまで表示させ全部埋めるためにはどのようにすれば良いでしょうか?

後、祝日を色付けするにはどうすればよいですか?

お願いします。

A 回答 (3件)

こんな感じでどうでしょ?



<?PHP
$month=$_REQUEST["num"];
if(preg_match("/^[0-9]{4}\/[0-9]{2}$/",$month,$matches)){
$date=date(strtotime($matches[0]."/01"));
}else{
$date=mktime(0,0,0,date("m"),1,date("Y"));
}
$y=date("Y",$date);
$m=date("m",$date);
$start_day=-date("w",$date)+1;
$end_day =date("d",mktime(0,0,0,$m+1,0,$y))+(6-date("w",mktime(0,0,0,$m+1,0,$y)));

$count=0;
for($i=$start_day;$i<=$end_day;$i++){
if($count==0) $tbody.="<tr>\n";
$tbody.="<td>".date("d",mktime(0,0,0,$m,$i,$y))."</td>";
if($count++==6){ $tbody.="\n</tr>\n"; $count=$count%7;}
}
$tbody="<tbody>\n{$tbody}</tbody>\n";
print <<<eof
<html>
<head>
<title>万年カレンダー</title>
<style>
.bgred{
background-color:#ffc0c0;
}
.bgblue{
background-color:#c0c0ff;
}
h1{
background-Color:#cccccc;
}
#table0{
width:300px;
border-collapse:collapse;
}
th,td{
border:1px solid #000000;
}
</style>
</head>
<body>
<h1>{$y}年{$m}月のカレンダー</h1>
<table id="table0">
<col class="bgred" /><col span="5" /><col class="bgblue" />
<thead>
<tr>
<th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th>
</tr>
</thead>
{$tbody}
</table>
<form>
<select name="num">
<option value="2008/01">2008年1月</option>
<option value="2008/02">2008年2月</option>
<option value="2008/03">2008年3月</option>
<option value="2008/04">2008年4月</option>
<option value="2008/05">2008年5月</option>
<option value="2008/06">2008年6月</option>
</select>
<input type="submit" value="go">
</form>
</body>
eof;
?>
    • good
    • 0
この回答へのお礼

またまたありがとうございます!
こんなやり方もあるのかと勉強になりました。

お礼日時:2008/01/29 16:59

こんな感じでやってみるといいかと。


ちなみに祝日は、ほいほい変わる物なので、管理用のテーブルを
用意しておいて、合致した場合は祝日・・・などの処理が必要です

<?PHP
$month=$_REQUEST["month"];
if(preg_match("/^[0-9]{4}\/[0-9]{2}$/",$month,$matches)){
$date=date(strtotime($matches[0]."/01"));
}else{
$date=mktime(0,0,0,date("m"),1,date("Y"));
}
$y=date("Y",$date);
$m=date("m",$date);
$start_day=-date("w",$date)+1;
$end_day =date("d",mktime(0,0,0,$m+1,0,$y))+(6-date("w",mktime(0,0,0,$m+1,0,$y)));
print "{$y}年{$m}月<br>";
print "日月火水木金土<br>";
$count=0;
for($i=$start_day;$i<=$end_day;$i++){
print date("d",mktime(0,0,0,$m,$i,$y));
if($count++==6) print "<br>";
$count=$count%7;
}
?>
<form>
<select name="month">
<option value="2008/01">2008年1月</option>
<option value="2008/02">2008年2月</option>
<option value="2008/03">2008年3月</option>
<option value="2008/04">2008年4月</option>
<option value="2008/05">2008年5月</option>
<option value="2008/06">2008年6月</option>
</select>
<input type="submit" value="go">
</form>

この回答への補足

?php
if($_GET['num']==''){
$today_year =date("Y");
$today_month=date("n");
$today_month1=date("n")+1;
$today_month2=date("n")+2;
}else{
$timestamp=mktime(0,0,0,date("n")+$_GET['num'],
date("Y"));
$today_year =date("Y",$timestamp);
$today_month=date("n",$timestamp);
$today_month1=date("n",$timestamp)+1;
$today_month2=date("n",$timestamp)+2;

}
$current=mktime(0,0,0,$today_month,1,$today_year);
$current1=mktime(0,0,0,$today_month1,1,$today_year);
$current2=mktime(0,0,0,$today_month2,1,$today_year);
$youbi=date("w",$current);
$youbi1=date("w",$current1);
$youbi2=date("w",$current2);
$hiniti=date("t",$current);
$hiniti1=date("t",$current1);
$hiniti2=date("t",$current2);

?>
<html>
<head>
<title>万年カレンダー</title>
</head>
<body>
<h1 style="background:#cccccc">
<?php print(date("Y年m月",$current)); ?>
のカレンダー</h1>
<table border="1" width="300">
<tr>
<th><font size='3' color='red'>日</th><th>月</th><th>火</th><th>水</th>
<th>木</th><th>金</th><th><font size='3' color='blue'>土</th>
</th>
<?php
for($i=1;$i<=$youbi+$hiniti;$i++){
if($i%7==1){print("<tr>");}
if($i>$youbi){
print("<td>".($i-$youbi)."</td>");
}else{
print("<td>w</td>");
}
if($i%7==0){
print("</tr>");
}
if($i>$hiniti){
print("<td>w</td>");

}
}
?>

の場合if($i>$hiniti){
print("<td>w</td>");をどういじれば、その月のカレンダーに翌月の分まで表示ができますか?とりあえず、今は空白にwの文字列を与えていますが、これは無視してください。

すいません。お願いします!

補足日時:2008/01/28 04:32
    • good
    • 0
この回答へのお礼

ありがとうございます。
まだ、プログラムを見ただけではどのように構成されているのかなどが分からない為、実際に見ながら勉強を続けたいと思います。

わざわざありがとうございました!

お礼日時:2008/01/28 03:00

ここが参考になると思います。



参考URL:http://w1.nirai.ne.jp/freeze/main/file11.html
    • good
    • 0
この回答へのお礼

色んなパターンのカレンダーが載っていて、他にも試してみようと思うのがありました!

ありがとうございます。

お礼日時:2008/01/28 03:01

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!