Php 当月份值超过12月时,如何更改日历中的年份?

Php 当月份值超过12月时,如何更改日历中的年份?,php,Php,我正在制作一个日历——我正在制作改变月份和年份的按钮 <?php } $id = preg_replace('#[^0-9]#i', '', $_GET['id']); $get_year = preg_replace('#[^0-9]#i', '', $_GET['year']); $teste = 1; $back = $id - 1; $next= $id + 1; if($id < 12){ }else{ ++$teste; ++$get_year; $

我正在制作一个日历——我正在制作改变月份和年份的按钮

<?php } 

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 

$get_year = preg_replace('#[^0-9]#i', '', $_GET['year']); 

$teste = 1;

$back = $id - 1; 

$next= $id + 1;

if($id < 12){

}else{
++$teste;
++$get_year;
$id = 1;
$next= $id;

}

?>
<a href="teste.php?id=<?= +$next?>&year=<?= $get_year; ?>"><button class="offersbtn">Next</button></a>
<?php

echo get_date($id,$get_year);
?>
每次单击“前进”按钮时,包含月份的变量将收到更多的1值

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); //the ID contains the months

$next= $id + 1; //forward button

if($id < 12){

}else{

$id = 1; //puts the id variable back to 1 again
$next = $id;
}
现在问题来了:当id到达12月12日时,我在更改年份时遇到了一些问题

我试过这个:

$years = date('Y');

$back = $id - 1; 
$next= $id + 1;

if($id < 12){

}else{

$id = 1;
$next= $id;
$years++;
}
获取日期功能:

  <?php function get_date($month, $year){

      switch ($month) {
        case "1":
        $ex_month = 'January';
        break;
        default:
        $ex_month = 'January';
      }

    ?>

    <div id="calendar-wrap">
    <div class="month-selection"><span><?= $ex_month . ' ' . $year; ?></span></div>

    <a href="?m=back"><<</a></td>
    </div>

    <div class="weeks">
    <?php $weeks = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');?>
    <div class="weeks-begin"><span><?= $weeks[0]; ?></span></div>
    <div><span><?= $weeks[1]; ?></span></div>
    <div><span><?= $weeks[2]; ?></span></div>
    <div><span><?= $weeks[3]; ?></span></div>
    <div><span><?= $weeks[4]; ?></span></div>
    <div><span><?= $weeks[5]; ?></span></div>
    <div><span><?= $weeks[6]; ?></span></div>
    <?php 

    $day = date('d');

    $running_day = date('w',mktime(0,0,0,$month,0,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;

    ?>

    </div>
    <br>
    <div class="c-col-1">

    <?php
    for($x = 0; $x < $running_day; $x++):   
    ?>
    <div class="c-empty"></div>
<?php
    endfor;
?>

<?php for($list_day = 1; $list_day <= $days_in_month; $list_day++): 

if($list_day == $day){

$div = '<div style="background: red;">';

}else{
$div = '<div>';
}
?>

<?= $div ?><span> <?= $list_day ?> </span><span class="resp-week"></span></div>

<?php 

endfor;


 ?>
</div>
</div>

<?php } 
?>



我已经做到了,下面是解决方案:

我创建了一个新参数来获取年份

<?php } 

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 

$get_year = preg_replace('#[^0-9]#i', '', $_GET['year']); 

$teste = 1;

$back = $id - 1; 

$next= $id + 1;

if($id < 12){

}else{
++$teste;
++$get_year;
$id = 1;
$next= $id;

}

?>
<a href="teste.php?id=<?= +$next?>&year=<?= $get_year; ?>"><button class="offersbtn">Next</button></a>
<?php

echo get_date($id,$get_year);
?>


从1月到12月,还是从10月到12月,问题出在哪里?如果是第一个选项,原因是您尚未实现该代码。每当id再次到达1月份时,我都要更改年份。有非常有用的类,例如使用php预构建的类。。。重写轮子有时会让人厌烦,我建议你仔细研究一下:)在这门课上,给日期加上一个月,这样它就会自动改变年份,这甚至不值得考虑
  <?php function get_date($month, $year){

      switch ($month) {
        case "1":
        $ex_month = 'January';
        break;
        default:
        $ex_month = 'January';
      }

    ?>

    <div id="calendar-wrap">
    <div class="month-selection"><span><?= $ex_month . ' ' . $year; ?></span></div>

    <a href="?m=back"><<</a></td>
    </div>

    <div class="weeks">
    <?php $weeks = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');?>
    <div class="weeks-begin"><span><?= $weeks[0]; ?></span></div>
    <div><span><?= $weeks[1]; ?></span></div>
    <div><span><?= $weeks[2]; ?></span></div>
    <div><span><?= $weeks[3]; ?></span></div>
    <div><span><?= $weeks[4]; ?></span></div>
    <div><span><?= $weeks[5]; ?></span></div>
    <div><span><?= $weeks[6]; ?></span></div>
    <?php 

    $day = date('d');

    $running_day = date('w',mktime(0,0,0,$month,0,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;

    ?>

    </div>
    <br>
    <div class="c-col-1">

    <?php
    for($x = 0; $x < $running_day; $x++):   
    ?>
    <div class="c-empty"></div>
<?php
    endfor;
?>

<?php for($list_day = 1; $list_day <= $days_in_month; $list_day++): 

if($list_day == $day){

$div = '<div style="background: red;">';

}else{
$div = '<div>';
}
?>

<?= $div ?><span> <?= $list_day ?> </span><span class="resp-week"></span></div>

<?php 

endfor;


 ?>
</div>
</div>

<?php } 
?>
<?php } 

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 

$get_year = preg_replace('#[^0-9]#i', '', $_GET['year']); 

$teste = 1;

$back = $id - 1; 

$next= $id + 1;

if($id < 12){

}else{
++$teste;
++$get_year;
$id = 1;
$next= $id;

}

?>
<a href="teste.php?id=<?= +$next?>&year=<?= $get_year; ?>"><button class="offersbtn">Next</button></a>
<?php

echo get_date($id,$get_year);
?>