从星期一开始带有正确的ColSpan的PHP日历

时间:2019-06-26 11:51:11

标签: php calendar

我正在尝试将在线PHP日历代码修改为星期一而不是星期日开始。

当我用PHP返回此日历时,我需要日历以下列数组:

星期一,星期二,星期三,星期四,星期五。

它从星期天开始并且可以正常运行,但是我想对此进行调整。

我已经尝试过自己,但是这是我无法提供的一些建议。非常感谢您的帮助。

function build_calendar($month,$year,$hide) 
{

    // Create array containing abbreviations of days of week.
    $daysOfWeek = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

    // 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

    $calendar = '';

    if ($hide == true) {
        $calendar .= "<div class='date_picker' style='display:none;' data-month='".$month."'>";
        $calendar .= "<div class='date_picker__wrap'>";
        $calendar .= "<a class='date_picker--prev'>Previous</a>";
        $calendar .= "<h2>$monthName, $year</h2>";
        $calendar .= "<a class='date_picker--next'>Next</a>";
        $calendar .= "</div>";
        $calendar .= "<table class='table table-bordered table-hover date_picker__calendar'>";
    } else {
        $calendar .= "<div class='date_picker date_picker__current' data-month='".$month."'>";
        $calendar .= "<div class='date_picker__wrap'>";
        $calendar .= "<a class='date_picker--prev'>Previous</a>";
        $calendar .= "<h2>$monthName, $year</h2>";
        $calendar .= "<a class='date_picker--next'>Next</a>";
        $calendar .= "</div>";
        $calendar .= "<table class='table table-bordered table-hover date_picker__calendar'>";
    }

    $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) { 
         $calendar .= "<td style='background: #f9f9f9;' colspan='$dayOfWeek'>&nbsp;</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";

      if (date('d') == $currentDay && date('n') == $month) {
           $add_class = ' today';
      } else {
           $add_class = '';
      }

      if (date('d') > $currentDay && date('n') == $month) {
           $add_class .= ' disable';
      }

      if ($dayOfWeek == 6 || $dayOfWeek == 0) {
           $calendar .= "<td class='day weekend".$add_class."' data-date='$date'><span class='actual_day'>$currentDay</span><span class='price'>+£50</span></td>";
      } else {
           $calendar .= "<td class='day weekday".$add_class."' data-date='$date'><span class='actual_day'>$currentDay</span><span class='price'>-£50</span></td>";
      };

      // Increment counters

      $currentDay++;
      $dayOfWeek++;

    }

    // Complete the row of the last week in month, if necessary

    if ($dayOfWeek != 7) { 

         $remainingDays = 7 - $dayOfWeek;
         $calendar .= "<td style='background: #f9f9f9;' colspan='$remainingDays'>&nbsp;</td>"; 

    }

    $calendar .= "</tr>";
    $calendar .= "</table>";
    $calendar .= "</div>";

    return $calendar;

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题:

    function build_calendar($month,$year,$hide) 
{

    // Create array containing abbreviations of days of week.
    $daysOfWeek = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

    // 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'] - 1;

    // Create the table tag opener and day headers
    $calendar = '';

    if ($hide == true) {
        $calendar .= "<div class='date_picker' style='display:none;' data-month='".$month."'>";
        $calendar .= "<div class='date_picker__wrap'>";
        $calendar .= "<a class='date_picker--prev'>Previous</a>";
        $calendar .= "<h2>$monthName, $year</h2>";
        $calendar .= "<a class='date_picker--next'>Next</a>";
        $calendar .= "</div>";
        $calendar .= "<table class='table table-bordered table-hover date_picker__calendar'>";
    } else {
        $calendar .= "<div class='date_picker date_picker__current' data-month='".$month."'>";
        $calendar .= "<div class='date_picker__wrap'>";
        $calendar .= "<a class='date_picker--prev'>Previous</a>";
        $calendar .= "<h2>$monthName, $year</h2>";
        $calendar .= "<a class='date_picker--next'>Next</a>";
        $calendar .= "</div>";
        $calendar .= "<table class='table table-bordered table-hover date_picker__calendar'>";
    }

    $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) {
         $calendar .= "<td style='background: #f9f9f9;' colspan='$dayOfWeek'>&nbsp;</td>"; 
    }

    if ($dayOfWeek == -1) {
      $calendar .= "<td style='background: #f9f9f9;' colspan='6'>&nbsp;</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) {

           $dayOfWeek = 0;
           $calendar .= "</tr><tr>";

      }

      $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);

      $date = "$year-$month-$currentDayRel";

      if (date('d') == $currentDay && date('n') == $month) {
           $add_class = ' today';
      } else {
           $add_class = '';
      }

      if (date('d') > $currentDay && date('n') == $month) {
           $add_class .= ' disable';
      }

      if ($dayOfWeek == 5 || $dayOfWeek == 6) {
           $calendar .= "<td class='day weekend".$add_class."' data-date='$date' data-week='$dayOfWeek'><span class='actual_day'>$currentDay</span><span class='price'>+£50</span></td>";
      } else {
           $calendar .= "<td class='day weekday".$add_class."' data-date='$date' data-week='$dayOfWeek'><span class='actual_day'>$currentDay</span><span class='price'>-£50</span></td>";
      };

      // Increment counters

      $currentDay++;
      $dayOfWeek++;

    }

    // Complete the row of the last week in month, if necessary

    if ($dayOfWeek != 7) { 

         $remainingDays = 7 - $dayOfWeek;
         $calendar .= "<td style='background: #f9f9f9;' colspan='$remainingDays'>&nbsp;</td>"; 

    }

    $calendar .= "</tr>";
    $calendar .= "</table>";
    $calendar .= "</div>";

    return $calendar;

}

相关问题