In this tutorial, we will share a useful Calendar Class, which developers can use to build powerful applications. The main benefit of using this PHP Calendar Class compare to others on the Internet is that this class comes with a hook. So you can use that hook to write plugins to accomplish your task that will save you from changing the existing codes of the Calendar Class.

Table of Content


  1. About
  2. Install
  3. Usage
  4. Write a plugin
  5. Download
  6. The End

Content


1. About

Calendar Class is built with plugin concept. It enables you to write your own plugins to achieve you task without changing the existing codes.
This class is pretty new, and we have plans to add more hooks for developers if needed, all you have to do is to leave a comment on comment section, so we will know what you need.
And of course, it is completely free, so please make use of it. And we will be very happy if you could leave a link of the application you built using our Calendar Class.

2. Install
  1. Download Calendar Class from https://github.com/dilab/php-calendar-class
  2. Include calendar.css file inside the header section of your file.
  3. Include calendar.php.
  4. That is all for installation. Read Usage section below to understand how to use it.
3. Usage
  1. Create an Calendar object.
  2. Call class function show() to output the calendar.
    You should be able to see a calendar showing on your current page:
  3. By default, Sunday will be marked as the first day of the week.
    However if you want to make Monday as the starting day of the week.
    You can call function setSundayFirst($param), and pass in $param as false.
  4. That is it. Now you have a simple and neat Calendar on your page.
    I hope you find it easy to use.
    To expand its usability, read section Write a plugin below to learn more.
4.Write a plugin

It is very common that you need to follow certain logic to change the content of the calendar cell.
Normally what we have to do is to change some parts of the codes to follow your logic.
Apparently, there are a lot of issues if you use approach above.
For example, it takes huge amount of time to understand and modify others' codes. And it is also very frustrated with updating the versions.
A huge benefit of the Calendar Class in this tutorial is that it comes with a hook which you can use to change the content of the cells. This means you do not have to modify any existing codes of the Calendar Class.

For demo purpose, we will show you a simple example, which is to highlight the cell if it is current date (TODAY).
This example will show you how to write a plugin, once you understand the method, it will be very easy to write your own plugins.

Right now, Calendar Class has one hook called "showCell", it is called before each cell's content is formed.
And the plugin's function which will be called by Calendar Class is called "update($cal)". And please note $cal will be the current instance of the Calendar Class.

  1. Create a plugin class with function update().
    We will call this class as 'HighlightToday' and its file name will be 'highlight_today.php'.
    1. Get current date of today.
    2. Compare today's date with each cell of the Calendar object.
    3. Set cell content to 'Today' if matching.
  2. Include 'highlight_today.php' in your page.
  3. Create a HighlightToday object.
  4. Attach HighlightToday plugin to Calendar Class object using 'showCell' hook.
  5. Now you should see today's calendar cell is marked as 'Today'.
5.Download

You can download this script from our github account.

6.The End

Thank you for reading this article, and if you have any encountered anything different, have a different solution or think our solution is wrong, do let us know in the comment section.
We will be very happy to hear that.

If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.