CodeIgniter日历类不会显示+1个月的正确日期

时间:2016-09-16 13:16:50

标签: php codeigniter calendar

我正在尝试展示三个callendars,一个用于本月,两个用于接下来的两个月。

这是我的评论控制器

public function index($year = null, $month = null, $day = null)
{
    if (empty($year)) {
        redirect('book/index/' . date('Y/m/d', time()));
    }

    $this->load->model('book/Booking_Model');

    // september
    $data['cal_one']   = $this->Booking_Model->generateCalendar($year, $month, $day);
    // october
    $data['cal_two']   = $this->Booking_Model->generateCalendar($year, date('m', strtotime('+1 months')), $day);
    // november
    $data['cal_three'] = $this->Booking_Model->generateCalendar($year, date('m', strtotime('+2 months')), $day);

    // to load the view
    $data['module'] = 'book';
    $data['view_file'] = 'select_date';
    $this->load->module('templates');
    $this->templates->frontend($data);
}

这就是我得到的输出

enter image description here

正如您所看到的,月份正确显示,但表格单元格显示的是同一个月,09(9月)。

如果你看左上方你可以看到我有var_dumped进入日历的$ month变量并且它们正确显示,09 ... 10 ... 11,但是在日历中它们都是09。

这是我的日历模型......

public function generateCalendar($year, $month, $day)
{
    var_dump($month);
    // styling the calendar
    $params = [
        'show_next_prev' => true,
        'next_prev_url'  => base_url() . 'schedule/index',
        'template'       => '
            {table_open}<table id="tt-calendar">{/table_open}

                {heading_row_start}<tr>{/heading_row_start}

                    {heading_previous_cell}
                        <th class="text-center">

                        </th>
                    {/heading_previous_cell}

                    {heading_title_cell}
                        <th class="text-center" colspan="{colspan}">
                            <h4>
                                {heading}
                            </h4>
                        </th>
                    {/heading_title_cell}

                    {heading_next_cell}
                        <th class="text-center">

                        </th>
                    {/heading_next_cell}

                {heading_row_end}</tr>{/heading_row_end}
                {week_row_start}<tr>{/week_row_start}

                    {week_day_cell}
                        <td>
                            <strong>
                                {week_day}
                            </strong>
                        </td>
                    {/week_day_cell}

                {week_row_end}</tr>{/week_row_end}
                {cal_row_start}<tr>{/cal_row_start}

                    {cal_cell_start}<td>{/cal_cell_start}

                    {cal_cell_start_today}<td id="tt-calendar-today">{/cal_cell_start_today}

                    {cal_cell_start_other}<td id="tt-calendar-test">{/cal_cell_start_other}

                    {cal_cell_content}
                        <div class="day" href="" onclick="return getDate(' . $year . ' + /' . $month . '/ + {day});">
                            {day} ' . $month . '
                        </div>
                    {/cal_cell_content}

                    {cal_cell_content_today}
                        <div class="day" href="" onclick="return getDate(' . $year . ' + /' . $month . '/ + {day});">
                            {day} ' . $month . '
                        </div>
                    {/cal_cell_content_today}

                    {cal_cell_no_content}
                        <div class="day" href="" onclick="return getDate(' . $year . ' + /' . $month . '/ + {day});">
                            {day} ' . $month . '
                        </div>
                    {/cal_cell_no_content}

                    {cal_cell_no_content_today}
                        <div class="day" href="" onclick="return getDate(' . $year . ' + /' . $month . '/ + {day});">
                            {day} ' . $month . '
                        </div>
                    {/cal_cell_no_content_today}

                    {cal_cell_blank}&nbsp;{/cal_cell_blank}

                    {cal_cell_other}{day}{/cal_cel_other}

                    {cal_cell_end}</td>{/cal_cell_end}

                    {cal_cell_end_today}</td>{/cal_cell_end_today}

                    {cal_cell_end_other}</td>{/cal_cell_end_other}

                {cal_row_end}</tr>{/cal_row_end}

            {table_close}</table>{/table_close}'
    ];

    // loading the calendar and passing the params and data
    $this->load->library('calendar', $params);
    return $this->calendar->generate($year, $month);
}

如果你看一下上面的模型,你可以看到我将$month变量传递给模型并且不可变地var_dumping它以确保它将“+1个月”传递到模型中。这是有效的...但是我在{cal_cell_content}标签中回应那个月,并且只在本月(9月)为三个日历中的每一个提供。

1 个答案:

答案 0 :(得分:0)

好的,这就是将来像我这样的人的答案。

问题原因

CodeIgniter加载实例化的ONCE库。所以我在这里试图获得日历&#39;库三次,实际上CodeIgniter看到我在9月份实例化了它,所以它确实为10月或11月实例化了(单例模式?)。

<强>解决方案

值得庆幸的是,CodeIgniter允许您为库分配自定义类名,例如

$this->load->library('calendar', $params, 'custom_name);
return $this->custom_name->generate($year, $month);

非常酷,所以现在我所要做的就是动态生成一个新名称&#39;对于我想要显示的每个月...所以我将名称设为$month变量。

$this->load->library('calendar', $params, $month);
return $this->$month->generate($year, $month);