24. März 2009 um 07:15 Uhr von admin · Trackback-URL
Der Kalender auf Basis von PHP ist beliebig blätterbar. Das einfügen und blättern ist auf Basis von JavaScript gelöst, dadurch muss man die Seite nicht neu laden.
Samstage und Sonntage haben verschiedene CSS-Klassen, dadurch können die Styles angepasst werden. Ebenso haben abgelaufene Monate extra CSS-Klassen um sie zu stylen.
Will man im Kalender blättern wird die JavaScript-Funktion open_calendar ausgeführt. Hier muss nur hinterlegt sein, dass per AJAX die Kalender-Datei mit den entsprechenden Parametern neu aufgerufen wird. Da ich dies mit jQuery gelöst habe, ist die Funktion speziell auf meinen Quellcode zugeschnitten und daher nicht allgemein verwendbar.
Ebenso verhält es sich mit der Funktion close_calendar, diese schließt mit jQuery einfach das DIV datepicker_popup.
31 ) { $day = 31; } // Aktueller Monat $month = intval( $get_date[1] ); if( $month < 10 && strlen( $month ) < 2 ) { $month = '0'. $month; } if( $month > 12 ) { $month = 12; } // Name des aktuellen Monats $month_name_array = array( $lang['general']['jan'], $lang['general']['feb'], $lang['general']['mar'], $lang['general']['apr'], $lang['general']['may'], $lang['general']['jun'], $lang['general']['jul'], $lang['general']['aug'], $lang['general']['sept'], $lang['general']['oct'], $lang['general']['nov'], $lang['general']['dec']); $month_name = $month_name_array[$month-1]; // Aktuelles Jahr $year = date('Y'); if( isset( $get_date[0] ) && $get_date[0] != '' ) { $year = intval( $get_date[0] ); } // Vorheriger Monat $prev_month = intval( $month - 1 ); if( $prev_month == 0 ) { $prev_month = 12; } else { if( $prev_month < 10 && strlen( $prev_month ) < 2 ) { $prev_month = '0'. $prev_month; } } // Vorheriges Jahr $prev_year = intval( $year - 1 ); // Naechster Monat $next_month = intval( $month + 1 ); if( $next_month < 1 ) { $next_month = 12; } elseif( $next_month > 12 ) { $next_month = '01'; } else { if( $next_month < 10 && strlen( $next_month ) < 2 ) { $next_month = '0'. $next_month; } } // Naechstes Jahr $next_year = intval( $year + 1 ); // Anzahl der Tage des aktuellen Monats $current_month_days = date("t", mktime(0, 0, 0, $month, $day, $year)); // Anzahl der Tage des vergangenen Monats $prev_month_days = date("t", mktime(0, 0, 0, $prev_month, $day, $year)); // Erster Tag des aktuellen Monats $current_month_1st_day = date("w", mktime(0, 0, 0, $month, 1, $year)); // Anzahl der Tage des Vormonats bis zum ersten Tag dieses Monats if( $current_month_1st_day == 0 ) { $skip_days = 6; } else { if( $current_month_1st_day != 1 ) { $skip_days = ( $current_month_1st_day - 1 ); } else { $skip_days = 0; } } // Anzahl der vollen Wochen und verbleibenden Tage des aktuellen Monats if( $skip_days != 0 ) { $full_weeks = floor( ( $current_month_days - ( 7 - $skip_days ) ) / 7 ); $days_remaining = ( $current_month_days - ( $full_weeks * 7 ) - ( 7 - $skip_days ) ); } else { $full_weeks = floor( ( $current_month_days ) / 7 ); $days_remaining = ( $current_month_days - ( $full_weeks * 7 ) ); } ?> <div id="datepicker_popup"> <div> <a class="close" href="javascript:close_calendar();"></a> <table border="0" cellspacing="0"> <tbody> <tr class="options"> <td class="prev_month"><a href="javascript:open_calendar('<?php if( $month == '01' ) { echo $prev_year; } else { echo $year; } ?>-<?php echo $prev_month; ?>-01');"><<</a></td> <td class="center" colspan="5"></td> <td class="next_month"><a href="javascript:open_calendar('<?php if( $month == 12 ) { echo $next_year; } else { echo $year; } ?>-<?php echo $next_month; ?>-01');">>></a></td> </tr> <tr> <th>Mo</th> <th>Di</th> <th>Mi</th> <th>Do</th> <th>Fr</th> <th>Sa</th> <th>So</th> </tr> <tr> <span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $prev_month_days_tmp .'</a></span> '."\n"; } else { $counter_new = $counter - $skip_days; $day = $counter_new; if( strlen( $counter_new ) < 2 ) { $day = '0'. $counter_new; } $class_day = ''; $table_end = "\n"; if( $i == 6 ) { $class_day = ' saturday'; } elseif( $i == 7 ) { $class_day = ' sunday'; $table_end = '</tr> <tr>'."\n"; } echo ' <td class="current'. $class_day .'"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $counter_new .'</a></span></td> '. $table_end; } } } $counter = ( $counter_new + 1 ); $counter_tmp = ( $full_weeks * 7 + $counter - 1); $rise = 0; for( $i = $counter; $i <= $counter_tmp; $i++ ) { $day = $i; if( strlen( $i ) < 2 ) { $day = '0'. $i; } $rise++; if( ( $i - $counter + 1 ) % 7 == 0 ) { echo ' <td class="current sunday"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $i .'</a></span></td> </tr> <tr>'."\n"; $rise = 0; } elseif( $rise == 6 ) { echo ' <td class="current saturday"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $i .'</a></span></td> '."\n"; } else { echo ' <td class="current"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $i .'</a></span></td> '."\n"; } } if( $days_remaining > 0 ) { $count_up = 0; for( $n = 1; $n <= 7; $n++ ) { if( $n <= $days_remaining ) { $day = $i; if( strlen( $i ) < 2 ) { $day = '0'. $i; } $class_day = ''; if( $n == 6 ) { $class_day = ' saturday'; } echo ' <td class="current'. $class_day .'"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $i .'</a></span></td> '."\n"; $i++; } else { $count_up++; $day = $count_up; if( strlen( $count_up ) < 2 ) { $day = '0'. $count_up; } $class_day = ''; if( $n == 6 ) { $class_day = ' saturday'; } elseif( $n == 7 ) { $class_day = ' sunday'; } echo ' <td class="gray'. $class_day .'"><span><a href="javascript:insert_date(\''. $year .'\', \''. $month .'\', \''. $day .'\');">'. $count_up .'</a></span></td> '."\n"; } } } ?></tr> </tbody></table> </div> </div>
Ich habs nur mal kurz überflogen – da lässt sich einiges einfacher gestalten…
Z.B. der Name des aktuellen Monats
$monate=array(‘Januar’,'Februar’,'März’,[...]);
$month_name=$monate[$month-1];
Der Artikel ist mit den folgenden Schlagworten versehen:
AJAX, JavaScript, Kalender, PHP
Es gibt derzeit 2 Kommentare zu diesem Eintrag. Wenn Du dazu auch etwas sagen möchtest, dann gib Deinen Senf hier ab.
Du kannst Dir auch die Antworten auf diesen Beitrag als RSS-Feed abonnieren.