上一个和下个月按钮不适用于PHP日历

时间:2013-02-25 20:07:37

标签: php calendar

我正在制作一个严格的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的测试文件中,因此您可以自己查看它的用途。

1 个答案:

答案 0 :(得分: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;和{{1}}(用于下一个和上一个按钮)。此外,您可以查看PHP Date并使用方法添加/减去一个月并格式化输出。