62,074
社区成员
发帖
与我相关
我的任务
分享
function test1()
{
echo $this->createCalendar(2008,11);
}
function createCalendar($year,$month)
{
//创建日历
$st=strtotime($year."-".$month."-01");//获得日期进行格式化
$arr=getdate($st);//准换成数组
$week=$arr["wday"];//得到当月的第一天是星期几
$daysNum=date("t",$st); //得到当月的天数
for($day=1;$day<=$daysNum;$day++)
{
$x=($day+$week-1)%7;//根据星期的规律循环x轴坐标
$y=($day+$week-1)/7;//根据(月/星期)循环Y轴坐标
for($j=0;$j<6;$j++)
{
for($i=0;$i<7;$i++)
{
$a[$x][$y]=$day;//开始赋值
}
}
}
//输出日历
$strTable="<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">";
$strTable.="<thead>
<th>星期天</th>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
<th>星期四</th>
<th>星期五</th>
<th>星期六</th>
</thead>";
for($i=0;$i<6;$i++)
{
$strTable.="<tr>";
for($j=0;$j<7;$j++)
{
$strTable.="<td>".$a[$j][$i]."</td>";
}
$strTable.="</tr>";
}
$strTable.="</table>";
//dump($a);
echo $week;
}
protected void Page_Load(object sender, EventArgs e)
{
createCalendar(2008,12);//输入参数进行测试
}
private void createCalendar(int year,int month)
{
int days = DateTime.DaysInMonth(year,month);//得到当月的天数
DateTime dt = new DateTime(year,month, 1);
int weeke =Convert.ToInt32(dt.DayOfWeek.ToString("d"));//得到当月的第一天是星期几
string[,] cal=new string[7,6];//创建一个二维数组来存放日期
for (int day= 1; day<= days;day++ )
{
int x = (day + weeke - 1) % 7;//根据星期的规律循环x轴坐标
int y = (day + weeke - 1) / 7;//根据(月/星期)循环Y轴坐标
for (int j = 0; j < 6;j++ )
{
for (int i = 0; i < 7;i++ )
{
cal[x, y] = day.ToString();//开始赋值
}
}
}
//输出
System.Text.StringBuilder st = new System.Text.StringBuilder();
st.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">");
st.Append("<thead><th>星期天</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></thead>");
for(int i=0;i<6;i++)
{
st.Append("<tr>");
for(int j=0;j<7;j++)
{
st.Append("<td>"+cal[j,i]+"</td>");
}
st.Append("</tr>");
}
st.Append("</table>");
Response.Write(st);
Response.End();
}