How to write a php calendar for a month and a year? Burden of calendar php

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

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

Realization of a month calendar in pure PHP without using mySQL, jQuery, etc. is given below:

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

". $ headings [$ head_day]."
"; $ calendar. ="";) $ calendar. =""; // set the beginning 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 ++;) // we have reached numbers, we will write them in the first line for ($ list_day = 1; $ list_day<= $days_in_month; $list_day++) { $calendar.= ""; // write the number into 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 in 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;)?>

The draw_calendar function receives the ordinal number of the month and year as input. The result of the function execution is the html-code of the calendar for the specified month. Using the above function is not difficult, and even a beginner in web development will be able to. The example below will display the calendar for January 2016.

January "16

The display of the signature for the calendar, including the name of the month and year, was deliberately not included in the function so that it could be freely changed, and possibly even removed altogether.

Php calendar for the year

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

However, in this case, you will need to create an array with a list of the names of the months in Russian, since you can get the names of the months from php only 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

The examples given in this post can be downloaded from the github.

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 site. 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();!}

Sep 14 2014

There are situations when you cannot use a component created in JavaScript. As a rule, these are cases when you need the ability not to automate the selection of dates in HTML form, but the ability to bind certain events that have occurred or planned to dates in the future. This can be the number of news items on a specific date, the number of products ordered 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 obtain data on the presence of certain events, but as practice shows, such a solution is not optimal. Therefore, this calendar component will be developed in PHP.

The 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 there is $ 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 based on your needs. You can download the calendar component.

Did you like the article? To share with friends: