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
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.
- Download Calendar Class from https://github.com/dilab/php-calendar-class
- Include calendar.css file inside the header section of your file.
- Include calendar.php.
- That is all for installation. Read Usage section below to understand how to use it.
- Create an Calendar object.
- Call class function show() to output the calendar.
You should be able to see a calendar showing on your current page:
- 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.
- 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.
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.
- Create a plugin class with function update().
We will call this class as 'HighlightToday' and its file name will be 'highlight_today.php'.
- Get current date of today.
- Compare today's date with each cell of the Calendar object.
- Set cell content to 'Today' if matching.
- Include 'highlight_today.php' in your page.
- Create a HighlightToday object.
- Attach HighlightToday plugin to Calendar Class object using 'showCell' hook.
- Now you should see today's calendar cell is marked as 'Today'.
You can download this script from our github account.
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.