繁体   English   中英

如何在 PHP 日历中显示当前月份和日期

[英]How to display Current Month and Day in PHP calendar

function build_calendar($month, $year){

    #Creating an array containing names of all days in the week
    $daysOfWeek = array('Sunday', 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

    # What is the first day of the month in question?
    $firstDayOfMonth = mktime(0,0,0,$month,1,$year);

    # How many days does this month contain?
    $numberDays = date('t',$firstDayOfMonth);

    # Retrieve some information about the first day of the
    # month in question.
    $dateComponents = getdate($firstDayOfMonth);

    # What is the name of the month in question?
    $monthName = $dateComponents['month'];

    # What is the index value (0-6) of the first day of the
    # month in question.
    $dayOfWeek = $dateComponents['wday'];

    # Create the table tag opener and day headers
     
    $datetoday = date('Y-m-d');
    $calendar = "<table class='table table-bordered'>";
    $calendar .= "<center><h2>$monthName $year</h2>";
    $calendar.= "<a class='btn btn-xs btn-primary' href='?month=".date('m', mktime(0, 0, 0, $month-1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month-1, 1, $year))."'>Previous Month</a> ";
    
    $calendar.= " <a class='btn btn-xs btn-primary' href='?month=".date('m')."&year=".date('Y')."'>Current Month</a> ";
    
    $calendar.= "<a href='?month=".date('m', mktime(0, 0, 0, $month+1, 1, $year))."&year=".date('Y', mktime(0, 0, 0, $month+1, 1, $year))."' class='btn btn-xs btn-primary'>Next Month</a></center><br>";
    
    $calendar .= "<tr>";

    # Create the calendar headers
    foreach($daysOfWeek as $day) {
        $calendar .= "<th  class='header'>$day</th>";
    } 
    
    # Create the rest of the calendar
    # Initiate the day counter, starting with the 1st.
    $currentDay = 1;
    $calendar .= "</tr><tr>";

     # The variable $dayOfWeek is used to
     # ensure that the calendar
     # display consists of exactly 7 columns.

    if($dayOfWeek > 0) { 
        for($k=0;$k<$dayOfWeek;$k++){
            $calendar .= "<td  class='empty'></td>"; 
        }
    }
    
     
    $month = str_pad($month, 2, "0", STR_PAD_LEFT);
    
    while ($currentDay <= $numberDays) {

         #Seventh column (Saturday) reached. Start a new row.

         if ($dayOfWeek == 7) {
             $dayOfWeek = 0;
             $calendar .= "</tr><tr>";
         }
          
         $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
         $date = "$year-$month-$currentDayRel";
         $dayname = strtolower(date('l', strtotime($date)));
         $eventNum = 0;
         $today = $date==date('Y-m-d')? "today" : "";
         if($date<date('Y-m-d')){
             $calendar.="<td><h4>$currentDay</h4> <button class='btn btn-danger btn-xs'>N/A</button>";
         }else{
             $calendar.="<td class='$today'><h4>$currentDay</h4> <a href='book.php?date=".$date."' class='btn btn-success btn-xs'>Book</a>";
         }
         
         
         $calendar .="</td>";

         #Increment counters

         $currentDay++;
         $dayOfWeek++;
     }
     
     # Complete the row of the last week in month, if necessary
     if ($dayOfWeek != 7) { 
        $remainingDays = 7 - $dayOfWeek;
        for($l=0;$l<$remainingDays;$l++){
            $calendar .= "<td class='empty'></td>"; 
        }
     }
     
    $calendar .= "</tr>";
    $calendar .= "</table>";
    return $calendar;
}

我在 2021 年 11 月 30 日创建了这个日历 function,但现在日期是 2021 年 1 月 12 日,但当前日期没有改变。 我尝试过调试,但似乎找不到问题所在。 我什至尝试重新启动我的 XAMPP 控制面板,但当前月份没有更新到 12 月。

我想要实现的是让日历自动显示当前日期和月份,这就是为什么我在当天添加了一个class='$today'的 class 的原因。 任何帮助将不胜感激。 谢谢你。

显示当前日期和错误当前日期的日历屏幕截图

一些反馈:

  • 每次调用date时,都会得到一个新的日期时间值。 虽然不太可能,但您的一天可能会在调用date之间发生变化,例如午夜前一毫秒,然后下一次调用可能在午夜之后。 您应该制作一个DateTime object 并在整个 function 中使用它。 您可以从 DateTime object 获取所需的所有信息。
  • 在您的if($date<date('Ym-d')){else条件行中,您没有检查> 我相信这是你的问题。
  • if ($dayOfWeek != 7) {逻辑似乎不正确。 您在while循环中循环遍历该月的所有天,然后在此if语句中再次遍历额外的天。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM