2013-02-25 51 views
2

我正在严格的PHP日历上工作,我的上一个和下一个按钮无法正常工作。到目前为止,我只有月份,年份和一个小表格。我还没有实际的日历。该表格由一个按钮组成,该按钮将日历带到当前的月份和年份。每个月都有一个下拉框,并且每年都有一个允许用户选择月份和年份的提交按钮。然后两个按钮去上一个/下一个月。我的代码是:上一个和下一个月的按钮不适用于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所以你可以亲眼看到它做什么。

+0

您的问题到底是什么?点击您网站上的“>>”和“<<”按钮,大致可以达到我期望的水平。 – 2013-02-25 20:13:14

+0

添加一些关于它应该做什么以及它在做什么的描述,而不是让我们试图弄清楚你的问题是什么。 – UnholyRanger 2013-02-25 20:13:33

+0

点击前一个按钮时,它在2月和1月之间交替。当下一个按钮被点击时,它不会在一月份重新开始并从那里开始。 – Vince 2013-02-25 20:18:22

回答

0

一个简单的解决方法是也计算是否需要使用前一年的时候,当你回去时你已经这样做了。

您可以使用$previous_year并使用该代替$year

这样

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

,使calucation就像你在上面做。

$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 Date并使用方法来添加/减去一个月并格式化输出。

+0

这很好用!我做了什么,是我删除了如果$月的条件,并将其更改为previous_month以及next_month。我认为这就是我做错了。 – Vince 2013-02-25 20:35:34

+0

不要忘记接受你的问题的答案:) – UnholyRanger 2013-02-25 20:51:39

相关问题