所有这些j都来自我的日历?

时间:2012-08-10 18:04:45

标签: php date calendar

所以最近我找到了一个漂亮的小教程,用于使用PHP和HTML制作日历。我使用了教程中的很多代码,因为它使我免于完成所有的数学运算。日期确实很糟糕。无论如何,压延机的工作原理只是我想要的,除了一个小细节。

在导航到上个月和下个月的按钮与当前月份名称之间是一行n。导航按钮和月份名称都在它们自己的表中,并且它们之间没有代码。在谷歌浏览器中,n是一大堆,如下所示:

nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

Internet Explorer 中,n位于一列中,每行一个n。我已经讨论了一段时间的代码,仍然无法弄清楚这些代码的来源或原因。我是新手在PHP中使用日期函数,所以我想知道这是否与此有关。

这是有问题的代码;

<?php
include'../../includes/head.inc';
include'../../includes/secure.inc';

$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];

$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>

<div class="calendar">
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="5" cellspacing="5">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
</tr>

<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>n";
if($i < $startday) echo "<td></td>n";
else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>n";
if(($i % 7) == 6 ) echo "</tr>n";
}
?>

</table>
</td>
</tr>
</table>
</div>

<?php
include'../../includes/footer.inc';
?>

代码有点草率,因为我现在正试图让它工作,并计划稍后进行所有格式化。我需要首先摆脱这些n。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:13)

接近代码的末尾:

if(($i % 7) == 0 ) echo "<tr>n";
if($i < $startday) echo "<td></td>n";
else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>n";
if(($i % 7) == 6 ) echo "</tr>n";

我会说有人在每行末尾看到\n并删除\但不删除n部分。