TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

PHP Calendar class with hooks

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.

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.

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.
   <html>
   <head>   
   <link href="calendar.css" type="text/css" rel="stylesheet" />
   </head>
   <body>  
   </body>
   </html>
  1. Include calendar.php.

    ?

   <html>
   <head>   
   <link href="calendar.css" type="text/css" rel="stylesheet" />
   </head>
   <body>  
   <?php
   include 'calendar.php';
   ?>
   </body>
   </html>
  1. That is all for installation. Read Usage section below to understand how to use it.

Usage

  1. Create an Calendar object.
   <html>
   <head>   
   <link href="calendar.css" type="text/css" rel="stylesheet" />
   </head>
   <body>  
   <?php
   include 'calendar.php';
   $calendar = new Calendar();
   ?>
   </body>
   </html>
  1. Call class function show() to output the calendar.

    ?

   <html>
   <head>   
   <link href="calendar.css" type="text/css" rel="stylesheet" />
   </head>
   <body>  
   <?php
   include 'calendar.php';
   $calendar = new Calendar();
   echo $calendar->show();
   ?>
   </body>
   </html>

You should be able to see a calendar showing on your current page: calendar class

  1. 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.
   <html>
   <head>   
   <link href="calendar.css" type="text/css" rel="stylesheet" />
   </head>
   <body>  
   <?php
   include 'calendar.php';
   $calendar = new Calendar();
   $calendar->setSundayFirst(false);
   echo $calendar->show();
   ?>
   </body>
   </html>
  1. 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.

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'.
class HighlightToday {
        public function update(Calendar $cal){
            $today = date('Y-m-d',time());          //1
             
            if($today==$cal->getCurrentDate()){     //2
                $cal->cellContent='Today';          //3
            }
             
        }
}
  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.

  4. Include 'highlight_today.php' in your page.

<html>
<head>   
<link href="calendar.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
 
include 'highlight_today.php';
 
echo $calendar->show();
?>
</body>
</html>
  1. Create a HighlightToday object.

?

<html>
<head>   
<link href="calendar.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
 
include 'highlight_today.php';
$highlightToday = new HighlightToday();
 
echo $calendar->show();
?>
</body>
</html>

Attach HighlightToday plugin to Calendar Class object using 'showCell' hook.

?

<html>
<head>   
<link href="calendar.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
 
include 'highlight_today.php';
$highlightToday = new HighlightToday();
$calendar->attachObserver('showCell',$highlightToday);
 
echo $calendar->show();
?>
</body>
</html>
  1. Now you should see today's calendar cell is marked as 'Today'. calendar class plugin

Download

You can download this script from our github account.

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.