Niet schrikken van de titel, ik zoek er geen :)
Naar aanleiding van het topic 'Datum probleem' en nadat ik op de SQL site een aantal opties zag, dacht ik, laat ik eens een PHP kalender maken.
Een hele tijd terug heb ik genoeg kalender gevonden maar ik vond ze lang niet altijd goed. Dit omdat het aantal weken vast stond met een for-loop op 6 (6 loops), en dat in de laatste loop werd gecontroleerd of er uberhaupt nog wel dagen in deze week voor kwamen. Dit vond ik echt slordig, maar ik had toen nog niet de skills die ik nu heb.
<?php
date_default_timezone_set('Europe/Amsterdam');
$iMonth = IsSet($_GET['month']) && Is_Numeric($_GET['month']) ? $_GET['month'] : Date('m');
$iYear = IsSet($_GET['year']) && Is_Numeric($_GET['year']) ? $_GET['year'] : Date('Y');
$sDate = MkTime(00, 00, 00, $iMonth, 1, $iYear);
$iFirstDay = Date('N', $sDate);
$iTotalDays = Date('t', $sDate);
echo '<table>
<tr>
<td style="font-weight: bold;">W</td>
<td style="font-weight: bold;">M</td>
<td style="font-weight: bold;">D</td>
<td style="font-weight: bold;">W</td>
<td style="font-weight: bold;">D</td>
<td style="font-weight: bold;">V</td>
<td style="font-weight: bold;">Z</td>
<td style="font-weight: bold;">Z</td>
</tr>';
$iDayOfMonth = 1;
For($iWeek = 1; $iWeek <= Ceil(($iTotalDays + $iFirstDay - 1) / 7); $iWeek++)
{
echo '
<tr>
<td>'.(Date('W', $sDate) + $iWeek - 1).'</td>';
For($iDayOfWeek = 1; $iDayOfWeek <= 7; $iDayOfWeek++)
{
$iDayOfWeek1 = $iFirstDay - $iDayOfWeek - 1;
If($iWeek == 1 && $iFirstDay > $iDayOfWeek)
{
echo '<td style="background-color: #ccc;">'.(Date('t', MkTime(00, 00, 00, $iMonth-1, 1, $iYear)) - ($iFirstDay - $iDayOfWeek - 1)).'</td>';
}
ElseIf($iDayOfMonth > $iTotalDays)
{
echo '<td style="background-color: #ccc;">'.($iDayOfMonth - $iTotalDays).'</td>';
$iDayOfMonth++;
}
Else
{
echo '<td>'.$iDayOfMonth.'</td>';
$iDayOfMonth++;
}
}
echo '
</tr>';
}
echo '
</table>
<a href="calendar.php?month='.($iMonth == 1 ? '12&year='.($iYear - 1) : ($iMonth - 1).'&year='.$iYear).'" title="Previous">Previous</a> - <a href="calendar.php?month='.($iMonth == 12 ? '1&year='.($iYear + 1) : ($iMonth + 1).'&year='.$iYear).'" title="Next">Next</a><br />
<a href="calendar.php?month='.$iMonth.'&year='.($iYear - 1).'" title="Previous">Previous</a> - <a href="calendar.php?month='.$iMonth.'&year='.($iYear + 1).'" title="Next">Next</a>
';
?>
Hier is dus mijn script, en ik zou graag wat commentaar willen :)
Wat kan beter etc.
Ik hang er nog geen DB achter want heb het net gemaakt.
En ook niet zeuren over dat het in een HTML-table is :), dat vond ik wel zo moeilijk om het even te maken. Het gaat dus puur om de PHP-code.
Preview: http://www.dennismertens.nl/calendar.php
Edit:
De variabele $iDay naar $iDayOfMonth verandert, duidelijker.
Edit:
Kom er net achter dat ik de week nummers mis. Even proberen in te bouwen :)
Edit:
Week nummer zit erin :)
M.v.g.
Dennis Mertens