Codeigniter Calendaring Class Library


Codeigniter Calendaring Class Library – Calendaring class library provide various functions that are used to create dynamically calendars. $this->load->library(‘calendar’); is used to load the library. This library serves to display and generate a calendar. Here in this tutorial, we are going to explain how to use calendaring class library.


Codeigniter calendaring class library.

Let us first see how to load calendaring class library and then use its function-

Load codeigniter calendaring class library

How to load calendaring class library:

Syntax of how to load this library.:-

 
$this->load->library('calendar');

Functions:-

There are following functions available in calendaring class library. Now we will explain one by one with example.

  • 1. Displaying a calendar
  • 2. Passing data to your calendar
  • 3. Setting display preferences
  • 4. Show next/previous month link
  • 5. Creating a calendar template

1. Displaying a calendar

Syntax of displaying a calendar is

Syntax of displaying a calendar is:-

generate([$year = ''[$month = ''[$data = array()]]])
$this->calendar->generate();

Parameters :-

  • $year (int) : Year
  • $month (int) : Month
  • $data (array) : Data to be shown in the calendar cells
  • Returns : HTML-formatted calendar
  • Return type : String

EXAMPLE

Here is simple example of displaying a calendar.

Displaying a calendar in codeigniter Example:-

//Controller
public function displaycalendar()
	{
		$this->load->library('calendar');
		echo $this->calendar->generate();	
	}

The output will be like this –

Codeigniter Calendaring Class library

2. Passing data to your calendar

EXAMPLE

Here is simple example of passing data to your calendar.

Passing data to your calendar in codeigniter example:-

//Controller
public function passingcalendar()
	{
		$this->load->library('calendar');
		$data = array 
		(
        2  => 'http://example.com/news/article/2017/03/02/',
        10  => 'http://example.com/news/article/2017/03/10/',
        17 => 'http://example.com/news/article/2017/03/17/',
        28 => 'http://example.com/news/article/2017/03/28/'
         );
         echo $this->calendar->generate(2017, 3, $data);   	
	}

The output will be like this –

Codeigniter Calendaring Class library

3. Setting display preferences

EXAMPLE

Here is simple example of setting display preferences.

Setting display preferences in codeigniter example:-

//Controller
public function setdisplay()
	{
		$attributes = array(
		'start_day'    => 'monday',
		'month_type'   => 'long',
		'day_type'     => 'short'
	);

	$this->load->library('calendar', $attributes);

	echo $this->calendar->generate();
	}

The output will be like this –

Codeigniter Calendaring Class library

4. Show next/previous month link

EXAMPLE

Here is simple example of show next/previous month link.

Show next/previous month link in codeigniter example:-

//Controller
public function showmonth()
	{
	$attributes = array('show_next_prev'  => TRUE,
        'next_prev_url'   => 'http://localhost/index.php/calendar/show/');
        $this->load->library('calendar', $attributes);
        echo $this->calendar->generate();	
	}

The output will be like this –

Codeigniter Calendaring Class library

5. Creating a calendar template

EXAMPLE

Here is simple example of creating a calendar template.

Creating a calendar template in codeigniter example:-

//Controller
public function temp()
	{
		$attributes['template'] = array
		(
        'table_open'           => '<table class="calendar">',
        'cal_cell_start'       => '<td class="day">',
        'cal_cell_start_today' => '<td class="today">'
        );

		$this->load->library('calendar', $attributes);

		echo $this->calendar->generate();

The output will be like this –

Class reference

There are following types of class reference in calendar classes.

1. Initialize

initialize([$config = array()])

This function is used to initialize the calendar preferences and containing display preferences.

2. Generate calendar

generate([$year = ''[$month = ''[$data = array()]]])

This function is used to generate the calendar.

3. Month name

get_month_name($month)

This function is used to generate text month name based on numeric month provider.

4. Day name

adjust_date($month, $year)
//like this
print_r($this->calendar->adjust_date(13, 2014));

This function is used to make sure that you have valid month/year.

5. Get total day

get_total_days($month, $year)
//like this
echo $this->calendar->get_total_days(7, 2017);

This function count total days of specific month.


User Ans/Comments

📖 Read More


Advertisements