PHP日历:将星期日的列移动到结尾

时间:2010-10-03 01:14:04

标签: php calendar

这是我从在线教程中获得的日历脚本。它工作正常但我想将星期日的列移到末尾(在星期六的列之后):

<?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

我该如何解决?或者,如何更改原始脚本以便我可以将星期日移动到列的末尾?

P.S。我还发现原始代码似乎有点问题/错误,如果你检查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!我能解决吗?谢谢!

2 个答案:

答案 0 :(得分:3)

我认为您需要更改第1天的第一天变量值

$firstday  = $thismonth['wday']; //from here
//adding
$firstday = ($firstday + 6) % 7; //shifting the 1st day

答案 1 :(得分:0)

您修改的脚本中存在两个问题:

循环从$ i = 0开始,但在$ i = 1之前不生成<tr>标记。因此第一列不在任何<tr>标记中。

此外,if($i < $firstday)需要六次才能生成六个空<td></td>标记,以便将日期移至右列。

要解决此问题,请从1开始循环,当星期日是第一天时,请设置$firstday = 7

<?php 
if($firstday == 0 ) $firstday = 7;
for ($i=1; $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";

}
?>