| |
| * Link führt ins Internet |
|

| |
Das aktuelle Jahr in Kalenderform darstellen
|
Beispielaufgabe
Es soll ein Kalender für das aktuelle Jahr ausgegeben werden, wobei der aktuelle Tag im aktuellen Monat hervorgehoben werden soll.
Beschreibung
Der folgende Code Snippet durchläuft zwei FOR-Schleifen. Die erste Schleife sorgt für das Durchlaufen aller 12 Monate, und schreibt jeden Monat in eine eigene Tabellenzelle (alle 3 Monate erfolgt ein Tabellenumbruch). Die zweite Schleife baut die Tabelle für die Monatsansicht auf. Hier wird zuerst die Tabelle mit Leerzellen gefüllt, bis zu dem Wochentag, an dem der Monat beginnt. Danach wird die Tabelle mit den Monatstagen gefüllt und der aktuelle Tag im aktuellen Monat besonders hervorgehoben.
<?PHP
$strBuildYear = '<table><tr>';
for ( $iCurrentMonth = 1; $iCurrentMonth <= 12; $iCurrentMonth++ )
{
// Tag der Woche 0 (fuer Sonntag) - 6 (fuer Samstag)
$iDayOfWeek = date ( 'w', mktime ( 0, 0, 0, $iCurrentMonth, 1,
date ( 'Y' ) ) );
if ( ! $iDayOfWeek )
{
// Woche beginnt mit Montag, deshalb Sonntag
// Wochentag 7 statt 0 zuweisen.
$iDayOfWeek = 7;
}
// Tabellenkopf der Monatsansicht. Zum Beispiel:
//
// April
// M D M D F S S
//
$strBuildYear .= '<td style="vertical-align: top;"><table style="bo' .
'rder: 2px solid #bcbcbc; background-color: #fff;">' .
'<tr><td colspan="7" style="text-align: center;">' .
date ( 'F', mktime ( 0, 0, 0, $iCurrentMonth, 1,
date ( 'Y' ) ) ) . '</td></tr><tr><td style="width: ' .
'17px;">M</td><td style="width: 17px;">D</td><td ' .
'style="width: 17px;">M</td><td style="width: 17px;">' .
'D</td><td style="width: 17px;">F</td><td style="' .
'width: 17px;">S</td><td style="width: 17px;">S</td>' .
'</tr><tr>';
$iLimit = date ( 't', mktime ( 0, 0, 0, $iCurrentMonth, 1,
date ( 'Y' ) ) );
$w = 0;
for ( $i = 1; $i <= ( $iLimit + $iDayOfWeek - 1 ); $i++ )
{
$iDayOfMonth = $i - $iDayOfWeek + 1;
if ( $i < $iDayOfWeek )
{
// Leerzellen schreiben, bis Monatsanfang
// auf den richtigen Tag der Woche faellt.
$strBuildYear .= '<td> </td>';
}
elseif ( $iDayOfMonth == date ( 'd' )
&& $iCurrentMonth == date( 'n' ) )
{
// Heutigen Tag, hervorheben.
$strBuildYear .= '<td style="background-color: #889fca; ' .
'font-weight: bold; text-decoration: ' .
'underline;">' . $iDayOfMonth . '</td>';
}
else
{
$strBuildYear .= '<td>' . $iDayOfMonth . '</td>';
}
if ( ! ( $i % 7 ) && $i != ( $iLimit + $iDayOfWeek - 1 ) )
{
// Am Ende eines jeden 7. Durchlaufs die Tabelle umbrechen.
$strBuildYear .= '</tr><tr>';
$w++;
}
}
if ( $w < 5 )
{
$strBuildYear .= '</tr><tr><td> </td>';
}
$strBuildYear .= '</tr></table></td>';
if ( ! ( $iCurrentMonth % 3 ) )
{
// Alle drei Monate eine </tr> einfügen.
$strBuildYear .= '</tr>';
}
if ( ! ( $iCurrentMonth % 3 ) && ( $iCurrentMonth % 12 ) )
{
// Alle 3 Monate, aber nicht nach dem
// Letzten Monat ein <tr> einfuegen.
$strBuildYear .= '<tr>';
}
}
$strBuildYear .= '</table>';
// Anwendungsbeispiel
print ( $strBuildYear );
?>
|
Ausgabebeispiel: Browseransicht
| January | | M | D | M | D | F | S | S | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | 15 | 16 | 17 | 18 | 19 | 20 | 21 | | 22 | 23 | 24 | 25 | 26 | 27 | 28 | | 29 | 30 | 31 | | |
| | February | | M | D | M | D | F | S | S | | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | | |
| | March | | M | D | M | D | F | S | S | | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | 29 | 30 | 31 | | |
| | April | | M | D | M | D | F | S | S | | | | | | | | 1 | | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 23 | 24 | 25 | 26 | 27 | 28 | 29 | | 30 |
| | May | | M | D | M | D | F | S | S | | | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | | |
| | June | | M | D | M | D | F | S | S | | | | | | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | 26 | 27 | 28 | 29 | 30 | | |
| | July | | M | D | M | D | F | S | S | | | | | | | | 1 | | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 23 | 24 | 25 | 26 | 27 | 28 | 29 | | 30 | 31 |
| | August | | M | D | M | D | F | S | S | | | | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 20 | 21 | 22 | 23 | 24 | 25 | 26 | | 27 | 28 | 29 | 30 | 31 | | |
| | September | | M | D | M | D | F | S | S | | | | | | | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | |
| | October | | M | D | M | D | F | S | S | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | 15 | 16 | 17 | 18 | 19 | 20 | 21 | | 22 | 23 | 24 | 25 | 26 | 27 | 28 | | 29 | 30 | 31 | | |
| | November | | M | D | M | D | F | S | S | | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | 29 | 30 | | |
| | December | | M | D | M | D | F | S | S | | | | | | | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | 31 |
|
|

|
|
|
|
|


|