2009-08-20 8 views
0

私はhttp://idea-palette.com/aleventcal/calendar.phpにPHPのカレンダーを持っています。 現在の月の一部ではない日がグレー表示されるようにしたい(これは現在)。前月と翌月の日であるように表示したい。月の合計日数より上の数値を新しい月の日数として表示するにはどうすればよいですか?

今のように、月の最初の日より前に表示される日は、負の数(-1、-2、-3など)とその月の最終日の後に表示される日数として表示されますちょうど続きますので、月が31日に終わると32,33,34などとなります。

私は、それが総日数よりも大きいかどうかを確認して何か他のことを行うことができる何らかのループで条件文を見つけようとしています。

for($i=0; $i< $total_rows; $i++) 
{ 
    for($j=0; $j<7;$j++) 
    { 
     $day++;     

     //if the current day is less or equal to the total days in the month   
     if($day>0 && $day<=$total_days_of_current_month) 
     { 
      $date_form = "$current_year/$current_month/$day"; 

      echo '<div class="date_has_event" href="#"><td'; 

      //If the date is today then give the td cell the 'today' class 
      if($date_form == $today) 
      { 
       echo ' class="today"'; 
      } 

      //check if any event stored for the date 
      if(array_key_exists($day,$events)) 
      { 
       //adding the date_has_event class to the <td> and close it 
       echo ' class="date_has_event">'.$day; 

       //adding the eventTitle and eventContent wrapped inside <span> & <li> to <ul> 
       echo '<div class="events"><ul>'.$events[$day].'</ul></div>'; 
      } 
     } 
     else //if the current day is less or more than the total days in the month 
     { 
      //then create a table cell with the current day of the mont 
      echo '<td class="padding">' . $day . '&nbsp;</td>'; h 
     } 
    } 
} 
:私が見る問題は、私はちょうどその日+ 1 $の代わりに、32、それだけで33

を読みますか。ここに私のコードですので、もし作成されているテーブルセルは、ループされていることです

答えて

1

ちょうどその日の現在の月の日数が正引く:

else //if the current day is less or more than the total days in the month 
{ 
    if($day > 1){ 
     echo '<td class="padding">' . ($day - $total_days_of_current_month) . ' </td>';  // the next month 
    } else { 
     echo '<td class="padding">' . $day . ' </td>';  //then create a table cell with the current day of the month 
    } 
} 
+0

これは、1か月の合計日数を超える数値に対してはうまく機能しましたが、月の最初の日より前に来る数値は31,30,29などとする必要がありますが、いくつかの月は30日を持ち、いくつかの月は31日を持っています。 – zeckdude

+0

クリスがどういう問題か分かりません。 http://ca2.php.net/manual/en/function.date.php 't'はその月の日数を表示します。ちょっと考えてみるだけです。すべての難しいものは既にPHPによって与えられています。 – mpen

1

は、ここでは、うまくいけば、そこからいくつかのアイデアを得ることができ、私が最近書いたカレンダー機能の一部です。

// $month is a UNIX timestamp 

// resetting to 1st of the month 
$date = getdate(mktime(0, 0, 0, date('n', $month), 1, date('Y', $month))); 

// resetting to first day in grid 
$date = getdate(strtotime("-$date[wday] days", $date[0])); 

$out = array(); 

$lastDay = mktime(0, 0, 0, date('n', $month), date('t', $month), date('Y', $month)); 

while ($date[0] <= $lastDay) { 

    $row = array(); 

    for ($x = 0; $x <= 6; $x++) { 
     $attr = array('class' => 'weekday '.low(date('D', $date[0]))); 
     if (date('n', $month) != $date['mon']) { 
      $attr['class'].= ' prevNextMonth'; 
     } 
     if (date('Y-m-d') == date('Y-m-d', $date[0])) { 
      $attr['class'].= ' today'; 
     } 

     $row[] = array($date['mday'], $attr); 

     $date = getdate(strtotime("+1 day", $date[0])); 
    } 

    $out[] = $row; 
} 

// makes table rows out of the array, considers the $attr array as well 
$out = $this->Html->tableCells($out); 
$out = sprintf('<table><tbody>%s</tbody></table>', $out); 
0

あなたはスティーブの条件を必要としません。代わりに、mktime関数は、範囲外の扱い

echo '<td class="padding">' . date("j",mktime(12,0,0,$current_month,$day,$current_year)) . ' </td>';

を使用すると、正確に何をしたいです次または前の月、にそれらを移動する私をさかのぼり。