×

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design.

Load a Calendar library

Before using the calendar class, you must load in the controller file by following the syntax:

$this -> load-> library ( ‘calendar’ );
Functions of the Calendaring class
  1. Display a calendar:

generate(): As the statement defines, this function is used to create a calendar of the current month and year based on the local server time.

Syntax

$this>load->library (‘calendar’);
 echo $this>calendar->generate(); 

If you want to show a calendar of a specific month and year, you have to pass the month and year as an argument in the generate() function of the calendar.

Syntax:

$this>load->library (‘calendar’);
 echo $this>calendar->generate(2015, 7); 

Example: Create a Learn.php file in application/controller folder and write the        following program:

Learn.php

<?php
 class Learn extends CI_Controller {
 public function generate_function()
 {   
 echo "<title> Tutorial and example </title>";
     $this->load->library('calendar');
     echo "This generate a calendar of local server";
 echo $this->calendar->generate();
 echo "<br>";
 echo "This generate a calendar of specific month and year ";
 echo $this->calendar->generate(2015, 7);
 }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/generate_function, it returns the output, as shown below.

CodeIgniter Calendaring Library

2.Passing data to the calendar cells:

Using this function, you can add data to calendar cells. This function uses an associative array that includes days of links and the array data populated on calendar cells.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

<?php
 class Learn extends CI_Controller {
 public function pass_function()
 {  
  echo "<title> Tutorial and example </title>";
     $this->load->library('calendar');
     $data = array(
     29  => 'http://tutorialandeaxmple.com',
     18  => 'http://tutorialandeaxmple.com', 
     3 => 'https://www.google.com',
     14 => 'http://localhost/codeIgniter-3.1.11/index.php/Learn/pass_function'
 );
 echo $this->calendar->generate(2020, 2, $data);
 }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/pass_function. It returns the output, as shown below.

CodeIgniter Calendaring Library

3.Setting Calendar preferences:

It enables you to set preferences in the calendar class, such as start date, day_type, month_type, etc.

There are seven preferences setting available in the calendar class.

Preferences Default Options
local_time time() None
start_day sunday You can provide any week day (Sunday, Monday, Tuesday, etc.)
month_type long Short, long
day_type abr Short, long, abr
show_next_prev FALSE It takes Boolean value TRUE or FALSE
next_prev_url Controller/method It takes a URL
show_other_days FALSE It takes Boolean value TRUE or FALSE

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

<?php
 class Learn extends CI_Controller {
 public function set_preference()
 {
     echo "<title> Tutorial and example </title>";
 $data = array(
     'start_day'    => 'monday',
     'month_type'   => 'short',
     'day_type'     => 'short'
 );
 $this->load->library('calendar', $data);
 echo $this->calendar->generate();
 }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/set_preference, it returns the output, as shown below.

CodeIgniter Calendaring Library

4.Showing Next/Previous Month links:

These methods allow you to dynamically create a forward or backward calendar through the use of Next and Previous links in the calendar class.

Example: Create a Learn.php file in application/controller folder and write the following program:

Learn.php

<?php
 class Learn extends CI_Controller {
 public function next_previous()
 {
     echo "<title> Tutorial and example </title>";
 $prefs = array(
     'show_next_prev'  => TRUE, 
     'next_prev_url'   => 'http://localhost/codeIgniter-3.1.11/index.php/Learn/next_previous'
 );
 $this->load->library('calendar', $prefs);
 echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
 }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/next_previous, it returns the output, as shown below.

CodeIgniter Calendaring Library

5.Creating a calendar Template:

Using this method, you can get 100% control over the design of the calendar. It uses key-value pairs in array methods to design the calendar.

Example: Create a Learn.php file in application/controller folder and write the following program:

            Learn.php

<?php
 class Learn extends CI_Controller {
 public function cal_temp()
             {
         $datas['template'] = array(
             'heading_row_start' => '<tr class = "heading_row_start">',
             'table_open'           => '<table class = "calendar">',
             'cal_cell_start'       => '<td class = "day">',
             'cal_cell_start_today' => '<td class = "today">'
     );
     $this->load->library('calendar', $datas);
     echo $this->calendar->generate();
     }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/cal_temp, it returns the output, as shown below.

CodeIgniter Calendaring Library

Class References

  1. generate(): It is used to create a calendar in a CodeIgniter application.

Syntax

generate ([ $year = ‘’ [, $month = ‘’ [, $data = array() ] ] ]);

Parameters:

$year: It defines the year.

$month: It takes month name in numeric.

$data: It defines the data that shown in the calendar cells.

2.get_month_name() : As the function defines, it is used to display the month name on a string format by taking a numeric value.

Syntax

get_month_name ($month);

$month:  It takes a numeric number to pass in get_month_name ().

3.get_day_names(): It is used to generate the days' name based on the type provided in the function. It contains three options to display the days' names, such as ‘long’, ‘short’, and ‘abr’. By default, it uses the ‘abr’ option to display the name of the day in the calendar.

Syntax

get_day_names ($day_type = ‘’ );

$day_type: It defines the type of days name as short, long or abr.

4.adjust_date(): As the name suggests, this function is used to adjust the date and year provided by the user. And if the year provided is not valid, it automatically increases or decreases the month and year.

Syntax

adjust_date ($month, $year);

It has two parameters:

$month: It takes month name in numeric.

$year: It takes year as a numeric.

5.get_total_days():  It is used to calculate the total number of days in a specified month.

Syntax

get_total_days ($month, $year);

It has two parameters:

$month: It takes month name in numeric.

$year: It takes Year as a numeric.

6.default_template(): As the name defines, it is used to set a default template in the calendar.

Syntax

default_template();

7.parse_template(): It is used to harvest data that is used in a template to display the calendar.

Syntax

parse_template();

Example: Create a Learn.php file in application/controller folder and write the following program:

            Learn.php

<?php
 class Learn extends CI_Controller {
     public function calendar_function()
     {  
  echo "<title> Tutorial and example </title>";
         $this->load->library('calendar');
 echo "<h3>Functions of calendar class</h3>"; 
         $y = '2022';
         $m = '07';
         echo "<strong> generate() function </strong>".$this->calendar->generate($y,$m);
         echo "<br>";
         $month = '05';
         $data= $this->calendar->get_month_name($month);
         echo "<strong> get_month_name() function </strong>".$data;
         echo "<br>";
         echo "<strong> get_day_names() function </strong>";
         print_r($this->calendar->get_day_names('long'));
         echo "<br>";
         echo "<strong> adjust_date() function </strong>";
         print_r($this->calendar->adjust_date(18, 2019));
         echo "<br>";
         echo "<strong> get_total_days() function </strong>".$this->calendar->get_total_days(5, 2020);
         echo "<br>";
         echo "<strong> default_template() function </strong>";
        print_r($this->calendar->default_template());
     }
 }
 ?> 

When you execute the above program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Learn/calendar_function, it returns the output, as shown below.

CodeIgniter Calendaring Library

Related Topics

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.