The php calendar is an integral part of many websites, frequently, this is one of numerous jQuery calendar plugins, but it can also be implemented using php. Today I will show you how to create a monthly calendar with the ability to scroll (left and right arrows) through the months using AJAX technology. Besides of ajax, this calendar has another important advantage, it is mobile-ready calendar with the responsive layout. Before we start writing code, I recommend that you look at our demo to see what we’re going to do.
Preview

Folder structure
In the beginning, let’s define a clear folder structure for all of our following files
- css – for all css files
- images – for all possible images
- templates – for all template files
Step 1. HTML
We are not going to use any specific template system (like Smarty), we are only about to use simple html templates with own replacement keys
templates/index.html
04 | <meta charset="utf-8" /> |
05 | <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> |
06 | <title>PHP AJAX Calendar</title> |
08 | <link href="css/styles.css" rel="stylesheet" type="text/css" /> |
This is a very simple template for our index page where we defined the parent container for our calendar. Another template will be used as an inner container of the calendar:
templates/calendar.html
01 | <div class="navigation"> |
02 | <a class="prev" href="index.php?month=__prev_month__" onclick="$('#calendar').load('index.php?month=__prev_month__&_r=' + Math.random()); return false;"></a> |
03 | <div class="title" >__cal_caption__</div> |
04 | <a class="next" href="index.php?month=__next_month__" onclick="$('#calendar').load('index.php?month=__next_month__&_r=' + Math.random()); return false;"></a> |
08 | <th class="weekday">sun</th> |
09 | <th class="weekday">mon</th> |
10 | <th class="weekday">tue</th> |
11 | <th class="weekday">wed</th> |
12 | <th class="weekday">thu</th> |
13 | <th class="weekday">fri</th> |
14 | <th class="weekday">sat</th> |
The reason is that in case of ajax requests we are not required to return everything, except the internal calendar content.
Step 2. PHP
The time for action
index.php
02 | list($iNowYear, $iNowMonth, $iNowDay) = explode('-', date('Y-m-d')); |
04 | if (isset($_GET['month'])) { |
05 | list($iMonth, $iYear) = explode('-', $_GET['month']); |
06 | $iMonth = (int)$iMonth; |
09 | list($iMonth, $iYear) = explode('-', date('n-Y')); |
12 | $iTimestamp = mktime(0, 0, 0, $iMonth, $iNowDay, $iYear); |
13 | list($sMonthName, $iDaysInMonth) = explode('-', date('F-t', $iTimestamp)); |
16 | $iPrevMonth = $iMonth - 1; |
17 | if ($iPrevMonth <= 0) { |
23 | $iNextMonth = $iMonth + 1; |
24 | if ($iNextMonth > 12) { |
29 | $iPrevDaysInMonth = (int)date('t', mktime(0, 0, 0, $iPrevMonth, $iNowDay, $iPrevYear)); |
31 | $iFirstDayDow = (int)date('w', mktime(0, 0, 0, $iMonth, 1, $iYear)); |
33 | $iPrevShowFrom = $iPrevDaysInMonth - $iFirstDayDow + 1; |
35 | $bPreviousMonth = ($iFirstDayDow > 0); |
37 | $iCurrentDay = ($bPreviousMonth) ? $iPrevShowFrom : 1; |
41 | for ($i = 0; $i < 6; $i++) { |
42 | $sCalTblRows .= '<tr>'; |
43 | for ($j = 0; $j < 7; $j++) { |
45 | if ($iNowYear == $iYear && $iNowMonth == $iMonth && $iNowDay == $iCurrentDay && !$bPreviousMonth && !$bNextMonth) { |
47 | } elseif (!$bPreviousMonth && !$bNextMonth) { |
50 | $sCalTblRows .= '<td class="'.$sClass.'"><a href="javascript: void(0)">'.$iCurrentDay.'</a></td>'; |
53 | if ($bPreviousMonth && $iCurrentDay > $iPrevDaysInMonth) { |
54 | $bPreviousMonth = false; |
57 | if (!$bPreviousMonth && !$bNextMonth && $iCurrentDay > $iDaysInMonth) { |
62 | $sCalTblRows .= '</tr>'; |
66 | '__prev_month__' => "{$iPrevMonth}-{$iPrevYear}", |
67 | '__next_month__' => "{$iNextMonth}-{$iNextYear}", |
68 | '__cal_caption__' => $sMonthName . ', ' . $iYear, |
69 | '__cal_rows__' => $sCalTblRows, |
71 | $sCalendarItself = strtr(file_get_contents('templates/calendar.html'), $aKeys); |
73 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_GET['month'])) { |
74 | header('Content-Type: text/html; charset=utf-8'); |
75 | echo $sCalendarItself; |
79 | '__calendar__' => $sCalendarItself, |
81 | echo strtr(file_get_contents('templates/index.html'), $aVariables); |
I tried to comment on every single line of code in order to let you understand the whole process. In the beginning we make date-related calculations with current date and requested dates. Then, we generate the calendar rows (days), finally, we replace the template keys to values. On Ajax requests, we return only the inner content ($sCalendarItself), otherwise – we display the whole page
Step 3. CSS
At the moment, our calendar does not look properly because it is just a bare html code. Let’s decorate our calendar
css/styles.css
03 | -moz-user-select: none; |
04 | border: 1px solid #EEEEEE; |
05 | border-radius: 6px 6px 6px 6px; |
07 | font-family: Arial,sans-serif; |
13 | #calendar .navigation { |
14 | background-color: #CC0000; |
15 | border: 1px solid #E3A1A1; |
16 | border-radius: 6px 6px 6px 6px; |
22 | #calendar .navigation .title { |
23 | background: none repeat scroll 0 0 transparent; |
24 | border-color: rgba(0, 0, 0, 0); |
30 | #calendar .navigation .prev, #calendar .navigation .next { |
31 | background-image: url(../images/nav.png); |
38 | #calendar .navigation .prev { |
39 | background-position: 0 0; |
42 | #calendar .navigation .next { |
43 | background-position: -24px 0; |
46 | #calendar .navigation .prev:hover, #calendar .navigation .next:hover { |
50 | border-collapse: collapse; |
65 | #calendar table td a { |
66 | background-color: #EEEEEE; |
67 | border: 1px solid #D8DCDF; |
74 | text-decoration: none; |
76 | #calendar table td a:hover { |
77 | background-color: #F6F6F6; |
78 | border: 1px solid #CDD5DA; |
81 | #calendar table td.current a { |
85 | #calendar table td.today a { |
86 | background-color: #FBF8EE; |
87 | border: 1px solid #FCD3A1; |
Step 4. Images
Only one small image is used in the styles of our calendar: nav.png

[sociallocker]
[/sociallocker]
Conclusion
That’s all for today, we have just prepared stylish responsive calendar. Thanks for your patient attention, and if you really like what we did today – share it with all your friends in your social networks using the form below.