鍍金池/ 問答/PHP  網(wǎng)絡(luò)安全/ php 寫了一個(gè)Calendar,但當(dāng)要跨年份的時(shí)候就不行了!

php 寫了一個(gè)Calendar,但當(dāng)要跨年份的時(shí)候就不行了!

$date = strtotime(date("Y-m-d"));

        $day = date('d', $date);
        $month = str_pad($_GET['m'],2,'0',STR_PAD_LEFT);
        $year = date('Y', $date);
        $firstDay = mktime(0,0,0,$month, 1, $year);
        $title = strftime('%m', $firstDay);
        $dayOfWeek = date('D', $firstDay);
        $daysInMonth = cal_days_in_month(0, $month, $year);
        $timestamp = strtotime('next Sunday');
        $weekDays = array('日', '一', '二', '三', '四', '五', '六');
        for ($i = 0; $i < 7; $i++){
          $timestamp = strtotime('+1 day', $timestamp);
        }
        $blank = date('w', strtotime("{$year}-{$month}-01"));
        
<td><?if($_GET['m']!='01'){?><a class="monthLink" href="?m=<?=str_pad($month-1,2,'0',STR_PAD_LEFT);?>&y=<?=$year;?>&region=<?=$_GET['region'];?>">← <?=$month-1;?>月</a><?}?></td>

我是用<?=$month-1;?><?=$month+1;?>來讓用戶選月份
但是到了01月的時(shí)候會(huì)變成00月
如能讓他能往上一年的十二月開始選?
以及往下一年的一月開始選?而不會(huì)有盡頭

回答
編輯回答
遺莣

第一次聽說用php來做日期選擇器的,用點(diǎn)擊鏈接的方式選擇日期體檢真的很差。

月份的問題,容易處理,當(dāng)01月的時(shí)候,點(diǎn)擊上一個(gè)月時(shí),你就把當(dāng)前的年份減1,月份變成12

同樣的道理,當(dāng)月份為12時(shí),點(diǎn)擊下一月時(shí),年份加1,月份變成01

2018年9月17日 02:23
編輯回答
放開她
if($month == 0)
{
    $year--;
    $month = 12;
}
if($month == 13)
{
    $year++;
    $month = 1;
}
2017年7月3日 10:07