改进php日历脚本,未检测到下个月的日子

时间:2015-03-24 13:03:02

标签: php html calendar

我正在尝试改进日历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 . '&amp;y=' . $y . '&amp;d=' . $day . '">&lt;</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 . '&amp;y=' . $x . '&amp;d=' . $day . '">&gt;</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;
}

screenshot calendar php example

解决了@Harry B !!!

见附图...

screenshot

1 个答案:

答案 0 :(得分:1)

给你的三个提示

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"; } }