dsizmmwnm56437180 2015-03-24 13:03
浏览 71
已采纳

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

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 . '&amp;y=' . $y . '&amp;d=' . $day . '">&lt;</a></span>' . "
";
    $calendar .= '    <span class="navi_title">' . $month_name . '/' . $year . '</span>' . "
";
    $calendar .= '    <span class="navi_next"><a class="next" href="' . $file . '?m=' . $next . '&amp;y=' . $x . '&amp;d=' . $day . '">&gt;</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;
}

screenshotcalendar php example

SOLVED thanks to @Harry B!!!

See the image attached...

screenshot

  • 写回答

1条回答 默认 最新

  • dongliao1948 2015-04-05 19:27
    关注

    Three tips for you

    if ($value == $time) { $new_time .= $key+1; } else { $new_time .= ""; } Could be written as if ($value == $time) { $new_time .= $key+1; } As .= "" will make no change to the string.

    if (($year % 4 == "") && ($month == 1)) { $days_in_month = $days_in_month+1; } else { $days_in_month = $days_in_month; } Could be written as if (!($year % 4) && $month === 1) { $days_in_month++; } As $days_in_month = $days_in_month; doesn't change your variable.

    and to fill the final 5 inactive days; you have a variable $k and you know you have 7 days (columns) per row. Once you are done with the current month, you need to continue looping to spit inactive divs until !(($k+1) % 7) or similar. First end of row means end of nice square box.

    Code untested and unloved:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化