php日历上一个按钮不断递增

时间:2014-08-04 08:19:45

标签: php button calendar

"谢谢你我已经解决了我的问题我认为这个工作"

我的代码只显示当月的星期一。

下一个按钮计算效果不佳。

假设它在12月(月)结束。

但它不断增加月份。

这是我的PHP代码。

<?php


$current_month = date("n");
$month = ($_GET['m']) ? $_GET['m'] : date("n");


$previous_month = ($month - 1);

$next_month = ($month + 1);

$year = ($_GET['y']) ? $_GET['y'] : date("Y");
$previous_year = $year;

if($month == 0)
{
  $month = 12;
  $year--;
}

if($month == 13)
{
  $month = 1;
  $year++;
}

if($previous_month == 0)
{
  $previous_month = 12;
  $previous_year--;
}

$startDate  = $year."-".$month."-01";
$endDate = $year."-".$month."-31";



$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calendar1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))

    echo '<td>'.date('d-M-y', $i).'</td>';   

echo '</tr>';   
echo("        </select>");
echo("        <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calendar1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo("        <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calendar1.php?m=". ($month + 1)."&y={$year}\"'/>");
echo("  </table>");
echo("<form>");

?>

请帮助解决问题。

3 个答案:

答案 0 :(得分:2)

这是您完整的工作代码

<?php
$current_month = date("n");

$month = (isset($_GET['m'])) ? $_GET['m'] : date("n");
$year = (isset($_GET['y'])) ? $_GET['y'] : date("Y");

$previous_month = ($month - 1);
$next_month = ($month + 1);

$previous_year = $year;
$next_year = $year;

if($previous_month==0)
{
    $previous_month = 12;
    $previous_year = $year-1;
}

if($next_month>12)
{
    $next_month = 1;
    $next_year = $year+1;
}

$startDate  = $year."-".$month."-01";
$endDate = $year."-".$month."-31";        

$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calender1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))

    echo '<td>'.date('d-M-y', $i).'</td>';   

echo '</tr>';   
echo("        </select>");
echo("        <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calender1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo("        <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calender1.php?m={$next_month}&y={$next_year}\"'/>");
echo("  </table>");
echo("<form>");
?>

答案 1 :(得分:0)

试试这个:

if ($month == 12) {
    $previous_month = $month;
    $next_month = 1;
} else {
    $previous_month = ($month - 1);
    $next_month = ($month + 1);
}

解决月问题

答案 2 :(得分:0)

这个问题可能不太老,但我希望它有助于微调答案,特别是在跨越一年的月份问题上

$previous_month = date("m", strtotime("-1 month", $year."-".$month."-01"));
$next_month = date("m", strtotime("+1 month", $year."-".$month."-01"));