从当前周开始,使用Codeigniter的日历库生成2周视图

时间:2012-02-10 16:14:57

标签: php codeigniter calendar

我正在尝试使用Codeigniter的日历库进行为期2周的观看,基本上我不需要一个完整的月份视图,而是希望从本周获得2周的视图。

enter image description here

上图显示了我想达到的相似性。在Codeigniter的模板方面,当生成时不提供这种功能。我不想使用jQuery UI的日历,因为我需要一个静态日历而不是依赖JS来执行此操作(特别是当用户禁用了JS时)。

是否有可以与Codeigniter合并的特定扩展库或可以执行此类日历视图的特定模板字符串?

2 个答案:

答案 0 :(得分:1)

您必须为此扩展日历库。

application/libraries中创建MY_Calendar.php

class MY_Calendar extends CI_Calendar {
    public function generate2weeks($year = '', $month = '', $data = array())
    {
        // code goes here
    }
}

查看CI_Calendar::generate()功能。您必须编写一个新函数generate2weeks(),其中基本上与generate()中的函数相同,但天数较少。我会复制generate()并从这里开始工作。也许仅仅尝试覆盖get_total_days就足够了。

答案 1 :(得分:0)

您需要扩展Calendar类并覆盖generate()方法。我花了一点时间来调整generate()函数,这就是我得到的。

function generate($year = '', $month = '', $data = array())
{
    // Set and validate the supplied month/year
    if ($year == '')
        $year  = date("Y", $this->local_time);

    if ($month == '')
        $month = date("m", $this->local_time);

    if (strlen($year) == 1)
        $year = '200'.$year;

    if (strlen($year) == 2)
        $year = '20'.$year;

    if (strlen($month) == 1)
        $month = '0'.$month;

    $adjusted_date = $this->adjust_date($month, $year);

    $month  = $adjusted_date['month'];
    $year   = $adjusted_date['year'];

    // Determine the total days in the month
    $total_days = $this->get_total_days($month, $year);

    // Set the starting day of the week
    $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
    $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];

    // Set the starting day number
    $local_date = mktime(12, 0, 0, $month, date('j'), $year);
    $date = getdate($local_date);
    $day  = $date["mday"];

    // Set the current month/year/day
    // We use this to determine the "today" date
    $cur_year   = date("Y", $this->local_time);
    $cur_month  = date("m", $this->local_time);
    $cur_day    = date("j", $this->local_time);

    $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;

    // Generate the template data array
    $this->parse_template();

    // Begin building the calendar output
    $out = $this->temp['table_open'];
    $out .= "\n";

    $out .= "\n";
    $out .= $this->temp['heading_row_start'];
    $out .= "\n";

    // "previous" month link
    if ($this->show_next_prev == TRUE)
    {
        // Add a trailing slash to the  URL if needed
        $this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/",  $this->next_prev_url);

        $adjusted_date = $this->adjust_date($month - 1, $year);
        $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
        $out .= "\n";
    }

    // Heading containing the month/year
    $colspan = ($this->show_next_prev == TRUE) ? 5 : 7;

    $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
    $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)." ".$year, $this->temp['heading_title_cell']);

    $out .= $this->temp['heading_title_cell'];
    $out .= "\n";

    // "next" month link
    if ($this->show_next_prev == TRUE)
    {
        $adjusted_date = $this->adjust_date($month + 1, $year);
        $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
    }

    $out .= "\n";
    $out .= $this->temp['heading_row_end'];
    $out .= "\n";

    // Write the cells containing the days of the week
    $out .= "\n";
    $out .= $this->temp['week_row_start'];
    $out .= "\n";

    $day_names = $this->get_day_names();

    for ($i = 0; $i < 7; $i ++)
    {
        $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
    }

    $out .= "\n";
    $out .= $this->temp['week_row_end'];
    $out .= "\n";

    // Build the main body of the calendar
    $limit = $day + 13;
    if ($limit > $total_days)
    {
        $total_days_left = $limit - $total_days;
    }
    while ($day <= $limit)
    {
        $out .= "\n";
        $out .= $this->temp['cal_row_start'];
        $out .= "\n";

        for ($i = 0; $i < 7; $i++)
        {
            if ($day > $total_days)
            {
                $day = 1;
                $limit = $total_days_left;
            }
            $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];

            if ($day > 0 AND $day <= $limit)
            {
                if (isset($data[$day]))
                {
                    // Cells with content
                    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
                    $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
                }
                else
                {
                    // Cells with no content
                    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
                    $out .= str_replace('{day}', $day, $temp);
                }
            }
            else
            {
                // Blank cells
                $out .= $this->temp['cal_cell_blank'];
            }

            $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];                 
            $day++;
        }
        $out .= "\n";
        $out .= $this->temp['cal_row_end'];
        $out .= "\n";
    }
    $out .= "\n";
    $out .= $this->temp['table_close'];

    return $out;
}

你可以在这里看到这个和原来的区别: http://www.diffnow.com/?report=51mkw

这将显示当前日期的2周。但是,当您调用日历库时,您需要使用start_daydate('l')来指定当天的strtolower(),如:

$prefs = array (
    'start_day'    => strtolower(date('l')),
);
$this->load->library('calendar', $prefs);

否则,日期将显示不正确。