Forrige og Næste måned i PHP Kalender
HalløjsaJeg har en kalender, som faktisk fungerer efter hensigten, men jeg kunne godt tænke mig, at forrige/næste måneds dage var med i de blanke felter, før/efter den egentlige måneds dage begynder.
Jeg har forsøgt at se, om jeg selv kunne hitte ud af det, men så hård er jeg ikke helt i PHP...
Er der nogen, der kan give en hånd med? På forhånd tak for hjælpen :-)
Her en hele min kode:
<?php
error_reporting(0);
$mysql = mysql_connect("localhost", "root", "");
mysql_select_db("booking_calender", $mysql) or die(mysql_error());
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Booking Calendar</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="author" content="Booking Calendar">
<meta name="description" content="Booking Calendar">
<meta name="keywords" content="Booking Calendar">
<link href="booking_calendar.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="calendar_frame">
<h1>Booking Calendar</h1>
<?php
// TODAY'S DATE.
$date = time();
// SEPERATE VARIABLES FOR DAY, MONTH, YEAR.
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
if ($_GET["m"]) {
$month = $_GET["m"];
$year = $_GET["y"];
} else {
$month = date('m', $date);
$year = date('Y', $date);
}
// FIRST DAY OF THE MONTH.
$first_day = mktime(0, 0, 0, $month, 1, $year);
// NAME OF THE MONTH
$name_of_month = date('F', $first_day);
// WEEKDAY OF THE FIRST DAY OF THE MONTH.
$day_of_week = date('D', $first_day) ;
switch ($day_of_week) {
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
// NUMBER OF DAYS IN CURRENT MONTH.
$days_in_month = cal_days_in_month(0, $month, $year);
// PREVIOUS AND NEXT MONTH.
if ($month == 12) {
$next_month = 1;
$next_year = $year +1;
} else {
$next_month = $month +1;
$next_year = $year;
}
if ($month == 1) {
$prev_month = 12;
$prev_year = $year -1;
} else {
$prev_month = $month -1;
$prev_year = $year;
}
?>
<p><a class="quick_navigation_links" href="<?php echo $_SERVER["PHP_SELF"] ."?m=". $prev_month ."&y=". $prev_year; ?>"><</a>
<span class="bold"><?php echo $name_of_month ." ". $year; ?></span>
<a class="quick_navigation_links" href="<?php echo $_SERVER["PHP_SELF"] ."?m=". $next_month ."&y=". $next_year; ?>">></a></p>
<table class="calendar_table">
<tr>
<td class="calendar_borders calendar_weekday_names">Sunday</td>
<td class="calendar_borders calendar_weekday_names">Monday</td>
<td class="calendar_borders calendar_weekday_names">Tuesday</td>
<td class="calendar_borders calendar_weekday_names">Wednesday</td>
<td class="calendar_borders calendar_weekday_names">Thursday</td>
<td class="calendar_borders calendar_weekday_names">Friday</td>
<td class="calendar_borders calendar_weekday_names">Saturday</td>
</tr>
<tr>
<?php
$day_count = 1;
// BLANK DAYS BEFORE THE MONTH.
while ($blank > 0) {
echo "<td class=\"calendar_borders calendar_no_date\"> </td>\n";
$blank = $blank-1;
$day_count++;
}
// SETS THE FIRST DAY OF THE MONTH TO 1.
$day_num = 1;
// COUNTS THE DAYS OF THE MONTH.
while ($day_num <= $days_in_month) {
// DISPLAYS TODAY AND BOOKED DATES IN DIFFERENT CSS STYLES.
echo"<td class=\"";
if (date("d") == $day_num and $month == date("m")) {
echo "calendar_borders calendar_weekdays_general calendar_weekday_today";
} else {
echo "calendar_borders calendar_weekdays_general calendar_weekdays";
}
// ---> DISPLAYS ALL BOOKED DATES.
$query = mysql_query("SELECT * FROM calendar WHERE '$year-$month-$day_num' BETWEEN startdate AND enddate") or die(mysql_error());
if ($row = mysql_fetch_array($query)) {
echo " calendar_weekdays_event";
}
// DISPLAYS TODAY AS BOOKED IN DIFFERENT CSS STYLE.
$query = mysql_query("SELECT * FROM calendar WHERE '$year-$month-$day_num' BETWEEN startdate AND enddate") or die(mysql_error());
if (($row = mysql_fetch_array($query)) and (date("d") == $day_num and $month == date("m"))) {
echo " calendar_weekday_today_event";
}
// ---> DISPLAYS FIRST AND LAST BOOKED DATE.
// $query = mysql_query("SELECT * FROM calendar WHERE startdate = '$year-$month-$day_num'") or die(mysql_error());
// if ($row = mysql_fetch_array($query)) {
// echo " calendar_weekdays_event";
// }
// $query = mysql_query("SELECT * FROM calendar WHERE enddate = '$year-$month-$day_num'") or die(mysql_error());
// if ($row = mysql_fetch_array($query)) {
// echo " calendar_weekdays_event";
// }
echo "\">$day_num</td>\n";
$day_num++;
$day_count++;
// STARTS A NEW ROW FOR EACH NEW WEEK.
if ($day_count > 7) {
echo "</tr>\n";
echo "<tr>\n";
$day_count = 1;
}
}
// BLANK DAYS AFTER THE MONTH.
while ($day_count > 1 and $day_count <= 7) {
echo "<td class=\"calendar_borders calendar_weekdays_general calendar_no_date\"> </td>\n";
$day_count++;
}
mysql_close();
?>
</tr>
</table>
</div>
</body>
</html>