PHP日历:将星期日的列移到最后
这是我从在线教程中获得的日历脚本。它工作正常,但我想将星期日的列移到末尾(星期六的列之后):
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$month_current = $_REQUEST["month"];
$year_current = $_REQUEST["year"];
$prev_year = $year_current;
$next_year = $year_current;
$month_previous = $month_current-1;
$month_next = $month_current+1;
if ($month_previous == 0 )
{
$month_previous = 12;
$prev_year = $year_current - 1;
}
if ($month_next == 13 )
{
$month_next = 1;
$next_year = $year_current + 1;
}
$timestamp = mktime(0,0,0,$month_current,1,$year_current);
$lastdate = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$firstday = $thismonth['wday'];
?>
<?php
for ($i=0; $i<($lastdate + $firstday); $i++)
{
if(($i % 7) == 0 ) echo "<tr>\n";
if($i < $firstday) echo "<td></td>\n";
else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";
if(($i % 7) == 6 ) echo "</tr>\n";
}
?>
我尝试将代码更改为:
<?php
for ($i=0; $i<($lastdate + $firstday); $i++)
{
if(($i % 7) == 1 ) echo "<tr>\n";
# if $i less than the first day (1), don't print the value of $i
if($i < $firstday) echo "<td></td>\n";
# print the value of $i
else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";
if(($i % 7) == 0 ) echo "</tr>\n";
}
?>
当第一天从星期日开始时,它不会在列中正确显示。例如: http://ec-ener.eu/dump/ index.php?month=8&year=2010
我该如何修复它?或者,如何更改原始脚本,以便可以将星期日移至列的末尾?
ps我还刚刚发现原始代码似乎有一点问题/错误,如果您检查它生成的html - tr和td -
<tr>
<td align='center' valign='middle' height='20px'>30</td>
<td align='center' valign='middle' height='20px'>31</td>
</table>
</td>
</tr>
它在其中有结束表,并且只有结束但没有开始。我相信原来的单个简单循环会生成一些无效的 html!我可以修复它吗?谢谢!
This is the calendar script I got from an online tutorial. It works fine but I want to move the column of Sunday to the end (after the column of Saturday):
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$month_current = $_REQUEST["month"];
$year_current = $_REQUEST["year"];
$prev_year = $year_current;
$next_year = $year_current;
$month_previous = $month_current-1;
$month_next = $month_current+1;
if ($month_previous == 0 )
{
$month_previous = 12;
$prev_year = $year_current - 1;
}
if ($month_next == 13 )
{
$month_next = 1;
$next_year = $year_current + 1;
}
$timestamp = mktime(0,0,0,$month_current,1,$year_current);
$lastdate = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$firstday = $thismonth['wday'];
?>
<?php
for ($i=0; $i<($lastdate + $firstday); $i++)
{
if(($i % 7) == 0 ) echo "<tr>\n";
if($i < $firstday) echo "<td></td>\n";
else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";
if(($i % 7) == 6 ) echo "</tr>\n";
}
?>
I tried to change the code into this:
<?php
for ($i=0; $i<($lastdate + $firstday); $i++)
{
if(($i % 7) == 1 ) echo "<tr>\n";
# if $i less than the first day (1), don't print the value of $i
if($i < $firstday) echo "<td></td>\n";
# print the value of $i
else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";
if(($i % 7) == 0 ) echo "</tr>\n";
}
?>
It then does not display properly in the column when the first day starts from Sunday. For instance: http://ec-ener.eu/dump/index.php?month=8&year=2010
How can I fix it? Alternatively, how can I change the original script so that I can move Sunday to the end of the columns?
p.s. I also just found out that the original code seems to have a bit problem/ bug, if you check the html - tr and td - it generates,
<tr>
<td align='center' valign='middle' height='20px'>30</td>
<td align='center' valign='middle' height='20px'>31</td>
</table>
</td>
</tr>
it has the closing table in the and there is only a closing but no opening. I believe that the original single simple loop generates some invalid html! can I fix it?? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要更改第一天的第一天变量值
I think you need to change the firstday variable value for 1st day
修改后的脚本中有两个问题:
循环从 $i = 0 开始,但直到 $i = 1 才生成
标记。因此第一列不在任何
标签。
此外,
if($i < $firstday)
需要为 true 六次才能生成六个空标记以将日期移动到右栏。
要解决此问题,请从 1 开始循环,当星期日是第一天时,设置
$firstday = 7
There are two problems in your modified script:
The loop is starting from $i = 0, but not generating a
<tr>
tag until $i = 1. So the first column isn't in any<tr>
tag.Also,
if($i < $firstday)
needs to be true six times to generate six empty<td></td>
tags to move the date to the right column.To fix it, start the loop from 1, and when Sunday is the first day, set
$firstday = 7