改进 php 日历脚本,未检测到下个月的日期
Improving php calendar script, the days of the next months are not detected
我正在尝试改进日历 php 脚本,此时我可以看到一切正常,但除了 接下来几个月的日子 未在网格中检测到...!我试着自己解决这个问题,但我运气不好,所以如果有人能帮我修复这个脚本就太好了!
<?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">' . "\n";
$calendar .= ' <div class="monheader">' . "\n";
$calendar .= ' <span class="navi_prev"><a class="prev" href="' . $file . '?m=' . $prev . '&y=' . $y . '&d=' . $day . '"><</a></span>' . "\n";
$calendar .= ' <span class="navi_title">' . $month_name . '/' . $year . '</span>' . "\n";
$calendar .= ' <span class="navi_next"><a class="next" href="' . $file . '?m=' . $next . '&y=' . $x . '&d=' . $day . '">></a></span>' . "\n";
$calendar .= ' </div>' . "\n";
$calendar .= ' <div class="dayheader">Mon</div>' . "\n";
$calendar .= ' <div class="dayheader">Tue</div>' . "\n";
$calendar .= ' <div class="dayheader">Wed</div>' . "\n";
$calendar .= ' <div class="dayheader">Thu</div>' . "\n";
$calendar .= ' <div class="dayheader">Fri</div>' . "\n";
$calendar .= ' <div class="dayheader">Sat</div>' . "\n";
$calendar .= ' <div class="dayheader">Sun</div>' . "\n";
# 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>' . "\n";
continue;
}
# Start the actual days
$n = $k-$new_time+1;
if ($n == $day) {
$calendar .= ' <div class="today">' . $n . '</div>' . "\n";
} else {
$calendar .= ' <div class="day">' . $n . '</div>' . "\n";
}
}
$calendar .= '</div>' . "\n";
# 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;
}
感谢@Harry B!!!
见附图...
给你的三个提示
if ($value == $time) {
$new_time .= $key+1;
} else {
$new_time .= "";
}
可以写成
if ($value == $time) {
$new_time .= $key+1;
}
因为 .= ""
不会更改字符串。
if (($year % 4 == "") && ($month == 1)) {
$days_in_month = $days_in_month+1;
} else {
$days_in_month = $days_in_month;
}
可以写成
if (!($year % 4) && $month === 1) {
$days_in_month++;
}
因为 $days_in_month = $days_in_month;
不会改变你的变量。
并填补最后 5 天不活跃;你有一个变量 $k
并且你知道每行有 7 天(列)。完成当前月份后,您需要继续循环以吐出不活动的 div,直到 !(($k+1) % 7)
或类似。行的第一个结尾意味着漂亮的方框的结尾。
未经测试且不受喜爱的代码:
for ($k = 1; $k < ($days_in_month+$new_time) || (($k-1) % 7); $k++) {
if ($k < $new_time || $k >= ($days_in_month+$new_time)) {
$calendar .= ' <div class="inactive"></div>' . "\n";
continue;
}
# Start the actual days
$n = $k-$new_time+1;
if ($n == $day) {
$calendar .= ' <div class="today">' . $n . '</div>' . "\n";
} else {
$calendar .= ' <div class="day">' . $n . '</div>' . "\n";
}
}
我正在尝试改进日历 php 脚本,此时我可以看到一切正常,但除了 接下来几个月的日子 未在网格中检测到...!我试着自己解决这个问题,但我运气不好,所以如果有人能帮我修复这个脚本就太好了!
<?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">' . "\n";
$calendar .= ' <div class="monheader">' . "\n";
$calendar .= ' <span class="navi_prev"><a class="prev" href="' . $file . '?m=' . $prev . '&y=' . $y . '&d=' . $day . '"><</a></span>' . "\n";
$calendar .= ' <span class="navi_title">' . $month_name . '/' . $year . '</span>' . "\n";
$calendar .= ' <span class="navi_next"><a class="next" href="' . $file . '?m=' . $next . '&y=' . $x . '&d=' . $day . '">></a></span>' . "\n";
$calendar .= ' </div>' . "\n";
$calendar .= ' <div class="dayheader">Mon</div>' . "\n";
$calendar .= ' <div class="dayheader">Tue</div>' . "\n";
$calendar .= ' <div class="dayheader">Wed</div>' . "\n";
$calendar .= ' <div class="dayheader">Thu</div>' . "\n";
$calendar .= ' <div class="dayheader">Fri</div>' . "\n";
$calendar .= ' <div class="dayheader">Sat</div>' . "\n";
$calendar .= ' <div class="dayheader">Sun</div>' . "\n";
# 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>' . "\n";
continue;
}
# Start the actual days
$n = $k-$new_time+1;
if ($n == $day) {
$calendar .= ' <div class="today">' . $n . '</div>' . "\n";
} else {
$calendar .= ' <div class="day">' . $n . '</div>' . "\n";
}
}
$calendar .= '</div>' . "\n";
# 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;
}
感谢@Harry B!!!
见附图...
给你的三个提示
if ($value == $time) {
$new_time .= $key+1;
} else {
$new_time .= "";
}
可以写成
if ($value == $time) {
$new_time .= $key+1;
}
因为 .= ""
不会更改字符串。
if (($year % 4 == "") && ($month == 1)) {
$days_in_month = $days_in_month+1;
} else {
$days_in_month = $days_in_month;
}
可以写成
if (!($year % 4) && $month === 1) {
$days_in_month++;
}
因为 $days_in_month = $days_in_month;
不会改变你的变量。
并填补最后 5 天不活跃;你有一个变量 $k
并且你知道每行有 7 天(列)。完成当前月份后,您需要继续循环以吐出不活动的 div,直到 !(($k+1) % 7)
或类似。行的第一个结尾意味着漂亮的方框的结尾。
未经测试且不受喜爱的代码:
for ($k = 1; $k < ($days_in_month+$new_time) || (($k-1) % 7); $k++) {
if ($k < $new_time || $k >= ($days_in_month+$new_time)) {
$calendar .= ' <div class="inactive"></div>' . "\n";
continue;
}
# Start the actual days
$n = $k-$new_time+1;
if ($n == $day) {
$calendar .= ' <div class="today">' . $n . '</div>' . "\n";
} else {
$calendar .= ' <div class="day">' . $n . '</div>' . "\n";
}
}