2

我正在使用严格的 PHP 日历,而我的上一个和下一个按钮无法正常工作。到目前为止,我只有月份、年份和一个小表格。我还没有实际的日历。该表单由一个按钮组成,该按钮将日历带到当前月份和年份。每个月和年都有一个下拉框,带有一个允许用户选择月和年的提交按钮。然后两个按钮用于转到上/下个月。我的代码是:

<?php
$month_numbers = array(1,2,3,4,5,6,7,8,9,10,11,12);
$months_of_year = array("January","February","March","April","May","June","July","August","September","October","November","December");
$days_of_week = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$month = ($_GET['m']) ? $_GET['m'] : date("n");
$current_month = date("n");
$year = ($_GET['y']) ? $_GET['y'] : date("Y");
$current_year = date("Y");
$previous_month = ($month - 1);
$next_month = ($month + 1);
if($month == 0)
{
  $month = 12;
  $year--;
}
if($month == 13)
{
  $month = 1;
  $year++;
}
$month_name = $months_of_year[$month - 1];
$first_day_of_month = new DateTime();
$first_day_of_month -> setDate($year,$month,1);
$first_day_of_month = $first_day_of_month -> format("w");
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'index.php?' method = 'get'>");
echo("  <table border = '1' cellspacing = '0' cellpadding = '2'>");
echo("    <thcolspan = '7'>
            {$month_name} {$year}
            <span class = 'formTopRight'>
              <input type = 'button' value = 'Today' onclick = 'location=\"index.php?m={$current_month}&y={$current_year}\"'/>
              <select name = 'm' id = 'selMonth'>");
for($a = 0; $a < count($months_of_year); $a++)
{
  echo("        <option value = '{$month_numbers[$a]}'>{$months_of_year[$a]}</option>");
}
echo("        </select>");
echo("        <select name = 'y' id = 'selYear'>");
for($b = ($current_year - 10); $b <= ($current_year + 10); $b++)
{
  echo("        <option value = '{$b}'>{$b}</option>");
}
echo("        </select>");
echo("        <input type = 'submit' name = 'dateChange' id = 'dateChange' value = 'Go'/>");
echo("        <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"index.php?m={$previous_month}&y={$year}\"'/>");
echo("        <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"index.php?m={$next_month}&y={$year}\"'/>");
echo("  </table>");
echo("<form>");
?>

我已将它上传到我的网站www.rgbwebd.net/tests上的测试文件中,这样您就可以亲眼看到它在做什么。

4

1 回答 1

0

一个简单的解决方法是计算您是否需要在返回时使用前一年,因为您在前进时已经这样做了。

您可以利用$previous_year和使用它而不是$year.

像这样

'location=\"index.php?m={$previous_month}&y={$previous_year}\"'/>");

并像您在顶部一样进行计算。

$previous_month = ($month - 1);
$previous_year = $year;
$next_month = ($month + 1);
if($month == 0)
{
  $month = 12;
  $year--;
}
if($month == 13)
{
  $month = 1;
  $year++;
}
if($previous_month == 0)
{
  $previous_month = 12;
  $previous_year--;
}

编辑

您不应该使用一个变量并在早期对其进行修改以支持两种不同的操作。因此,您用于$month填写上一个和下一个按钮。您应该将它们分成$next_month; $next_year;$previous_month; $previous_year;(用于下一个和上一个按钮)。此外,您可能会查看PHP 日期并使用方法来添加/减去一个月并格式化输出。

于 2013-02-25T20:23:41.757 回答