I'm trying to improve a calendar php script, at this moment I can see all working fine but except for the days of the next months that are not detected in the grid...! I tried solving the issue by myself but I have not lucky so if someone can help me to fix this script would be great!
<?php
function calendar($file) {
if ((isset($_GET['d'])) ? $day = $_GET['d'] : $day = date("d"));
if ((isset($_GET['m'])) ? $month = $_GET['m'] : $month = date("m"));
if ((isset($_GET['y'])) ? $year = $_GET['y'] : $year = date("Y"));
# Create arrays for the calendar
$months_days = array("31","28","31","30","31","30","31","31", "30","31","30","31");
$months_name = array("Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec");
$days_array = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
# Removes the 0 from start of month (can't find array key with 0)
if (strlen($month) == 1) {
$month = str_replace("0","",$month);
} else {
$month = $month;
}
# Reset month to the array key match (array starts at 0)
$month = $month-1;
# Find the days in the month
$days_in_month = $months_days[$month];
# And convert the month number to name
$month_name = $months_name[$month];
# $m is used to find month
$m = $month+1;
# Find the first day of the month
$time = date("M D Y H:i:s", mktime(0, 0, 0, $m, 1, $year));
$first_day = explode(" ",$time);
$time = $first_day[1];
##### Create the links to next and previous months
$next = $month+2;
$x = $year;
# If month is 13 then new year
if ($next == 13) {
$next = 1;
$x = $x+1;
}
$prev = $month;
$y = $year;
# If month is 0, then previous year
if ($prev == 0) {
$prev = 12;
$y = $y-1;
}
# Build the calendar with css
# Links to next and previous month
$calendar = "";
$calendar .= '<div class="calendar">' . "
";
$calendar .= ' <div class="monheader">' . "
";
$calendar .= ' <span class="navi_prev"><a class="prev" href="' . $file . '?m=' . $prev . '&y=' . $y . '&d=' . $day . '"><</a></span>' . "
";
$calendar .= ' <span class="navi_title">' . $month_name . '/' . $year . '</span>' . "
";
$calendar .= ' <span class="navi_next"><a class="next" href="' . $file . '?m=' . $next . '&y=' . $x . '&d=' . $day . '">></a></span>' . "
";
$calendar .= ' </div>' . "
";
$calendar .= ' <div class="dayheader">Mon</div>' . "
";
$calendar .= ' <div class="dayheader">Tue</div>' . "
";
$calendar .= ' <div class="dayheader">Wed</div>' . "
";
$calendar .= ' <div class="dayheader">Thu</div>' . "
";
$calendar .= ' <div class="dayheader">Fri</div>' . "
";
$calendar .= ' <div class="dayheader">Sat</div>' . "
";
$calendar .= ' <div class="dayheader">Sun</div>' . "
";
# Checks for leap years and add 1 to February
if (($year % 4 == "") && ($month == 1)) {
$days_in_month = $days_in_month+1;
} else {
$days_in_month = $days_in_month;
}
$new_time = "";
# Find how many blank spaces at beginning of the month
foreach ($days_array as $key => $value) {
if ($value == $time) {
$new_time .= $key+1;
} else {
$new_time .= "";
}
}
# Loop through the days in the month
for ($k = 1; $k < ($days_in_month+$new_time); $k++) {
if ($k < $new_time) {
$calendar .= ' <div class="inactive"></div>' . "
";
continue;
}
# Start the actual days
$n = $k-$new_time+1;
if ($n == $day) {
$calendar .= ' <div class="today">' . $n . '</div>' . "
";
} else {
$calendar .= ' <div class="day">' . $n . '</div>' . "
";
}
}
$calendar .= '</div>' . "
";
# Reset for link to today
$d = date("d");
$m = date("m");
$y = date("Y");
return $calendar;
}
$file = basename($_SERVER['PHP_SELF']);
echo calendar($file);
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Calendar</title>
<link href="calendar.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php include("calendar.php"); ?>
</body>
</html>
calendar.css
@charset "utf-8";
.navi_prev {
width: 20px;
float: left;
}
.navi_next {
width: 20px;
float: right;
}
.navi_title {
font-weight: bold;
font-size: 20px;
}
.prev {
color: white;
font-size: 20px;
text-decoration: none;
font-weight: bold;
}
.next {
color: white;
font-size: 20px;
text-decoration: none;
font-weight: bold;
}
.calendar {
width: 600px;
}
.calendar .day,
.calendar .today,
.calendar .inactive,
.calendar .dayheader,
.calendar .monheader {
float: left;
height: 80px;
width: 80px;
border: 1px solid #333333;
}
.calendar .monheader {
font-weight: bold;
color: #FFFFFF;
text-align: center;
height: 20px;
width: 572px;
background-color: #333333;
}
.calendar .dayheader {
font-weight: bold;
color: #FFFFFF;
text-align: center;
height: 20px;
background-color: #000000;
}
.calendar .day {
background-color: #FFFFCC;
}
.calendar .today {
background-color: #FFCC00;
border-color: #CC0000;
}
.calendar .inactive {
background-color: #666666;
}
SOLVED thanks to @Harry B!!!
See the image attached...