php日历上一个按钮持续递增
“谢谢你,我已经解决了我的问题,我认为这一个有效” 我有一个只在一个月的星期一显示的代码 “下一步”按钮计算不好 应该在12(月)结束 但是这个月它一直在增加 这是我的PHP代码php日历上一个按钮持续递增,php,button,calendar,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 = (
<?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>");
?>
试试这个:
if ($month == 12) {
$previous_month = $month;
$next_month = 1;
} else {
$previous_month = ($month - 1);
$next_month = ($month + 1);
}
这就解决了月份问题这是您的完整工作代码
<?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>");
?>
这个问题可能有点老掉牙,但我希望它有助于调整答案,尤其是在跨年度的月份问题上
$previous_month = date("m", strtotime("-1 month", $year."-".$month."-01"));
$next_month = date("m", strtotime("+1 month", $year."-".$month."-01"));