I would like to write about working with calendars in CodeIgniter. We use dates in many parts of our application. Consider an example where you have to display “Thoughts of the Day” in calendar format. When you click on a date, it takes you to a page where it display a thought. To do this using CodeIgniter is very simple. As I mentioned about libraries and the advantages of using them in my earlier posts, today I would like to explain about “Calendar” library.
Lets see how to use it. In order to use the library we have to first load it. The code to load the library is
And displaying the calendar is pretty simple. Just add the following line
This two lines will display a calendar of the current year and month. The result will be like the following screenshot.
If you want to display for a specific year and month, lets say 2010 march, we can do that by passing year and month as parameters to the generate method.
If in case, you wish to change the start day to Monday we can do that as well. We can customize it as we want. To make Monday as the first day make the following changes.
$prefs = array (
'start_day' => 'monday',
'month_type' => 'long',
'day_type' => 'short'
If you wish to add next and previous link to the calendar, we can do that by
$prefs = array (
'show_next_prev' => TRUE,
'next_prev_url' => 'http://www.geeks.gallery/calendar/'
echo $this->calendar->generate($this->uri->segment(2), $this->uri->segment(3));
Coming to the important section, linking the dates to separate page, the code is
$data = array(
3 => 'http://www.geeks.gallery/wordpress-theme-installation/',
7 => 'http://www.geeks.gallery/how-to-remove-index-php-from-codeigniter-url/',
13 => 'http://www.geeks.gallery/tabactivity-in-android/',
26 => 'http://www.geeks.gallery/change-the-favicon-in-your-website/'
The result would look like
Thanks for reading.
Don’t forget to subscribe to Geeks.Gallery, to get free emails to your inbox.
Subscribe to Geeks.Gallery
Latest posts by Nagarajan (see all)