2013-10-17 64 views
0

我有一個網頁,我把4個簡單的日曆顯示未來4個月的日期。我已經得到了顯示當前月份的代碼,但我不確定如何編輯代碼以顯示第二,第三和第四個月。以下是我用來查找當前月份的代碼:下個月PHP日曆

<table> 
<?php 
$today = date("d"); // Current day 
$month = date("m"); // Current month 
$year = date("Y"); // Current year 
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month 

$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month 

$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month 
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month 
$laststart = $start - 1; // Days of previous month in calander 

$counter = 1; 
$nextMonthCounter = 1; 

if($start > 5){ $rows = 6; }else {$rows = 5; } 
for($i = 1; $i <= $rows; $i++){ 
    echo '<tr class="week">'; 
    for($x = 1; $x <= 7; $x++){    

     if(($counter - $start) < 0){ 
      $date = (($lastmonth - $laststart) + $counter); 
      $class = 'class="blur"'; 
     }else if(($counter - $start) >= $days){ 
      $date = ($nextMonthCounter); 
      $nextMonthCounter++; 

      $class = 'class="blur"'; 

     }else { 
      $date = ($counter - $start + 1); 
      if($today == $counter - $start + 1){ 
       $class = 'class="today"'; 
      } 
     } 


     echo '<td '.$class.'><a class="date">'. $date . '</a></td>'; 

     $counter++; 
     $class = ''; 
    } 
    echo '</tr>'; 
} 
?> 
</table> 

需要更改哪些變量才能使其適用於未來幾個月?

+1

您必須瞭解該代碼或至少嘗試理解。你也可以付錢去改變它。 –

+0

@ElonThan我明白這一點。我知道一些簡單的事情,比如將一些變量改爲月+ 1或類似的東西,但我不能確切地知道它在哪裏,網上沒有很多教程 – user2737457

回答

1

要獲得下個月,您必須更改$month變量,例如。像這樣date("m", strtotime('+1 month'));

+0

謝謝,它是strtotime(),但我得到理念。唯一的問題是今年。我已經做了4個月,但顯然1月份再次出現在2013年,我如何計算它是第二年自動? – user2737457

+0

獲取當前日期並從中提取年份,日期('Y',strtotime($ date))'然後比較當前年份。 –

+0

謝謝,但最後我用了這個方法:'$ year = date(「Y」,strtotime(「+ 1 months」));' - 似乎工作正常 – user2737457