The Zap Cal Library is an open source PHP library for reading and writing iCalendar files. The library has been in development for over 10 years supporting the Zap Calendar program, an open source application for the Joomla CMS, and more recently the iCalendar validator project at website. It is now available as a standalone library for PHP developers.

The Zap Calendar iCalendar Library is a PHP library for supporting the iCalendar (RFC 5545) standard. Several examples of reading and writing iCalendar files are included in the library

This PHP library is for reading and writing iCalendar formatted feeds and files. Features of the library include:

  • Read AND write support for iCalendar files
  • Object based creation and manipulation of iCalendar files
  • Supports expansion of RRULE to a list of repeating dates
  • Supports adding timezone info to iCalendar file

All iCalendar data is stored in a PHP object tree. This allows any property to be added to the iCalendar feed without requiring specialized library function calls. With power comes responsibility. Missing or invalid properties can cause the resulting iCalendar file to be invalid..

Here is an example of a PHP program to create a single event iCalendar file:

$title = "(!LANG:Simple Event"; // date/time is in SQL datetime format $event_start = "2020-01-01 12:00:00"; $event_end = "2020-01-01 13:00:00"; // create the ical object $icalobj = new ZCiCal(); // create the event within the ical object $eventobj = new ZCiCalNode("VEVENT", $icalobj->curnode); // add title $eventobj->addNode(new ZCiCalDataNode("SUMMARY:" . $title)); // add start date $eventobj->addNode(new ZCiCalDataNode("DTSTART:" . ZCiCal::fromSqlDateTime($event_start))); // add end date $eventobj->addNode(new ZCiCalDataNode("DTEND:" . ZCiCal::fromSqlDateTime($event_end))); // UID is a required item in VEVENT, create unique string for this event // Adding your domain to the end is a good way of creating uniqueness $uid = date("Y-m-d-H-i-s") . "@demo.. $uid)); // DTSTAMP is a required item in VEVENT $eventobj->addNode(new ZCiCalDataNode("DTSTAMP:" . ZCiCal::fromSqlDateTime())); // Add description $eventobj->addNode(new ZCiCalDataNode("Description:" . ZCiCal::formatContent("This is a simple event, using the Zap Calendar PHP library. " .."))); // write iCalendar feed to stdout echo $icalobj->export();!}

On the eve of the new year, a very trivial task arose - to make a calendar site, where for each month it was necessary to display a calendar for a month. The first step in solving the problem was the search for ready-made solutions. After sorting through a dozen solutions given on the Internet, the choice was made. Some versions turned out to be non-working at all, some were too cumbersome - they would have to be pretty “sawed off” to get the desired result. So, let's look at how to write a simple calendar in php.

The basis of the script was found on the Internet, the errors were fixed, something was completed, in particular, the functionality of highlighting days off with a separate css class was added.

Monthly calendar implementation in pure PHP without using mySQL, jQuery, etc. is given below:

"; // display days of the week $headings = array("Mon","Tue","Wed","Thurs","Fri","Sat","Sun"); $calendar.= " "; for($head_day = 0; $head_day<= 6; $head_day++) { $calendar.= ""; $calendar.= "

".$headings[$head_day]."
"; $calendar.= ""; ) $calendar.= ""; // set the start of the week to Monday $running_day = date("w",mktime(0,0,0,$month,1,$year)); $running_day = $running_day - 1; if ($running_day == -1) ( $running_day = 6; ) $days_in_month = date("t",mktime(0,0,0,$month,1,$year)); $day_counter = 0; $days_in_this_week = 1; $dates_array = array(); // first line of the calendar $calendar.= " "; // display empty cells for ($x = 0; $x< $running_day; $x++) { $calendar.= ""; $days_in_this_week++; ) // got to the numbers, we will write them in the first line for($list_day = 1; $list_day<= $days_in_month; $list_day++) { $calendar.= ""; // write the number to the cell $calendar.= "
".$list_day."
"; $calendar.= ""; // reached the last day of the week if ($running_day == 6) ( // close the line $calendar.= ""; // if the day is not the last of the month, start the next line if (($day_counter + 1) != $days_in_month) ( $calendar.= " "; ) // reset the counters $running_day = -1; $days_in_this_week = 0; ) $days_in_this_week++; $running_day++; $day_counter++; ) // display empty cells at the end of the last week if ($days_in_this_week< 8) { for($x = 1; $x <= (8 - $days_in_this_week); $x++) { $calendar.= " "; ) ) $calendar.= ""; $calendar.= ""; return $calendar; ) ?>

As input, the draw_calendar function receives the ordinal number of the month and year. The result of the function execution is the html-code of the calendar for the given month. Using the above function is not difficult, and even a beginner in web development can do it. The example below will display the calendar for January 2016.

January "16

The output of the calendar caption, which includes the name of the month and the year, was deliberately not included in the function so that it could be freely changed, and possibly completely removed.

php calendar for a year

From the above function, you can easily get a calendar php script for a year, and for any. To do this, it is enough to go through all the months in a loop and call the function for displaying the calendar for the month for each of them.

However, in this case, you will need to create an array with a list of month names in Russian, since you can only get the names of months from php in English.

The code in this case will be as follows:

"January", 1 => "February", 2 => "March", 3 => "April", 4 => "May", 5 => "June", 6 => "July", 7 => " August", 8 => "September", 9 => "October", 10 => "November", 11 => "December"); for ($month = 1; $month<= 12; $month++) { ?>

"16

You can download the examples in this post from github.

Sep 14 2014

There are situations when you can not use a component created in JavaScript. As a rule, these are the cases when you need the ability not to automate the date selection in the HTML form, but the ability to link some past or planned events to dates in the future. This may be the number of news on a certain date, the number of ordered goods or purchases made in an online store, etc. Those. in such cases, it becomes obvious that there is a need for a connection between the calendar and the database in order to obtain certain information for a certain date. Of course, it would be possible to create a calendar in JavaScript and use Ajax to get data about the presence of certain events, but as practice shows, this solution is not optimal. Therefore, this calendar component will be developed in PHP.

Style settings for the calendar are contained in the file calendar.css. And the PHP script is in the file calendar class.

To work with the calendar, you need to add the following code to the module you need:

// Include the module require_once (dirname (__FILE__) . "/calendar.class.php"); // Array with event dates in Unix format $Events = array(1409518800, 1409778000,1410210000,1410901200,1411592400); // Get the date if $date = (isset($_REQUEST["date"]))? $_REQUEST["date"] : "" ; // Create a calendar object $calendar = new Calendar($date, $Events); // Display the calendar echo $calendar->ShowCalendar();

You can modify the source code of this calendar according to your needs. You can download the calendar component.