我正在制作日历 - 我正在制作按钮以更改月份和年份。
每次单击前进按钮时,包含月份的变量将获得更多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;
}
按钮代码:
<a href="test.php?id=<?= +$next?>"><button>Next</button></a>
调用该函数:
echo get_date($id,2014);
现在问题出现了:当id到达12月(12)时,我在改变年份方面遇到了一些问题。
我试过了:
$years = date('Y');
$back = $id - 1;
$next= $id + 1;
if($id < 12){
}else{
$id = 1;
$next= $id;
$years++;
}
我打电话给这个函数:
echo get_date($id,$years);
get_date函数:
<?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 }
?>
答案 0 :(得分:0)
我已经做到了,这里是解决方案:
我创建了一个新参数来获得年份。
<?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);
?>