<?php
$Jan = 31;
$Mar = 31;
$Apr = 30;
$May = 31;
$Jun = 30;
$Jul = 31;
$Aug = 31;
$Sep = 30;
$Oct = 31;
$Nov = 30;
$Dec = 31;
$leap = date('L');
if ($leap) {
$Feb = 29;
} else {
$Feb = 28;
}
$months = Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$daysinweek = Array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$currmth = date('n');
if ($currmth == 12) {
$nextmth = 'Jan';
$newyr = date('Y') + 1;
} else {
$nextmth = $months[$currmth];
}
if ($_POST['lastday']) {
$month = $nextmth;
$date = 1;
if ($_POST['lastday'] == 'Sun') {
$firstday = 'Mon';
} else {
$dayk = array_search($_POST['lastday'], $daysinweek);
$firstday = $daysinweek[$dayk + 1];
}
$dayofweek = $firstday;
} else {
$month = date('M');
$date = date('j');
$dayofweek = date('D');
}
if ($_POST['newyr']) {
$year = $newyr;
} else {
$year = date('Y');
}
// get distance to first day of the month
$tmpdate = $date;
$dist = 0;
if ($tmpdate != 1) {
while ($tmpdate != 1) {
$tmpdate--;
$dist++;
}
$tmpdate = $date - 1;
$key = array_search($dayofweek, $daysinweek);
$key = $key - 1;
while ($dist > 0) {
if ($key == -1) {
$key = 6;
}
$cal[$tmpdate] = $daysinweek[$key];
$key--;
$tmpdate--;
$dist--;
}
$cal[$date] = $dayofweek;
$tmpdate = $date + 1;
$key = array_search($dayofweek, $daysinweek) + 1;
while ($tmpdate <= $$month) {
if ($key == 7) {
$key = 0;
}
$cal[$tmpdate] = $daysinweek[$key];
$key++;
$tmpdate++;
}
} else {
$key = array_search($dayofweek, $daysinweek);
$cal[$date] = $dayofweek;
$date = $date + 1;
$key = $key + 1;
while ($date <= $$month) {
if ($key == 7) {
$key = 0;
}
$cal[$date] = $daysinweek[$key];
$key++;
$date++;
}
}
echo "<table><tr><td align='center' colspan=7>", $month, " ", $year, "</td></tr><tr><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td><td>S</td></tr>";
$key = 1;
$posinweek = 0;
while ($key <= $$month) {
if ($posinweek == 0) {
echo '<tr>';
}
if ($posinweek == 7) {
echo '</tr>';
$tr = $tr + 1;
$posinweek = 0;
}
if ($posinweek <= 6) {
if ($cal[$key] == $daysinweek[$posinweek]) {
echo '<td>', $key, '</td>';
$key++;
$posinweek++;
} else {
echo '<td></td>';
$posinweek++;
}
}
}
if ($tr != 4) {
for ($i = 6; $posinweek <= $i; $posinweek++) {
echo '<td> </td>';
}
} else {
$posinweek = 0;
echo '<tr>';
for ($i = 6; $posinweek <= $i; $posinweek++) {
echo '<td> </td>';
}
echo '</tr>';
}
echo '</tr><tr>';
if ($_POST['lastday']) {
echo "<td align='left' colspan=7><form name='myform' action='calendar.php' method='POST'><input type='hidden' name='currmth' value='yes'><input type='submit' value='Prev'></form></td>";
} else {
if ($nextmth == 'Jan') {
echo "<td align='right' colspan=7><form name='myform' action='calendar.php' method='POST'><input type='hidden' name='lastday' value='", $cal[$$month], "'><input type='hidden' name='newyr' value='yes'><input type='submit' value='Next'></form></td>";
} else {
echo "<td align='right' colspan=7><form name='myform' action='calendar.php' method='POST'><input type='hidden' name='lastday' value='", $cal[$$month], "'><input type='submit' value='Next'></form></td>";
}
}
echo '</tr>';
echo '</table>';
?>