Calendaring Class CodeIgniter

Facebooktwitterredditpinterestlinkedinmailby feather

Calendaring Class

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 Class

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

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

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:

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

Learn.php

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.

Calendaring Class CodeIgniter
  • 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

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.

Calendaring Class CodeIgniter
  • 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

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.

Calendaring Class CodeIgniter
  • 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

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.

Calendaring Class CodeIgniter
  • 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

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.

Calendaring Class CodeIgniter

Class References

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

Syntax

Parameters:

$year: It defines the year.

$month: It takes month name in numeric.

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

  • 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

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

  • 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

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

  • 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

It has two parameters:

$month: It takes month name in numeric.

$year: It takes year as a numeric.

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

Syntax

It has two parameters:

$month: It takes month name in numeric.

$year: It takes Year as a numeric.

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

Syntax

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

Syntax

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

            Learn.php

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.

Calendaring Class CodeIgniter
Facebooktwitterredditpinterestlinkedinmailby feather