Merry Christmas Everyone!!!
In the spirit of Christmas I thought it would be nice to share a simple calendar script written in PHP Object-Oriented Programming style. It also uses namespaces in PHP meaning if you have other classes with the same methods or variables there will be no conflict with them.
SPECIAL NOTE
[php]require_once ‘lib/classes/calendar/DisplayCalendar.php’;[/php]
You have to have the correct folder structure and file name (obviously) in order for this to work, if you use your own file structure make sure to make the appropriate changes.
The script is actually pretty short and can be modified pretty easily (Well, at least I think so. ;D)
Here is the index.php page:
[php]<?php
date_default_timezone_set(‘America/Detroit’); // Set the Default Time Zone:
require_once ‘lib/classes/calendar/DisplayCalendar.php’;
/* Uses namespaces */
use jrpepp_production\DisplayCalendar as Calendar;
$mydate = new Calendar(“NOW”);
?>
<!doctype html>
The DisplayCalendar.php file:
[php]<?php
namespace jrpepp_production;
use DateTime;
class DisplayCalendar {
private $date;
public $output = \NULL;
protected $prev = \NULL;
protected $current = \NULL;
protected $next = \NULL;
protected $month = \NULL;
protected $day = \NULL;
protected $year = \NULL;
protected $days = \NULL;
protected $prevMonth = \NULL;
protected $nextMonth = \NULL;
protected $calendar = array();
protected $dispCalendar = array();
protected $theForm = \NULL;
protected $alphaDay = array(0 => “Sun”, 1 => “Mon”, 2 => “Tue”, 3 => “Wed”, 4 => “Thu”, 5 => “Fri”, 6 => “Sat”);
/* Constructor to create the calendar */
public function __construct($date) {
$this->output = $this->generateCalendar($date);
}
/* Last month filler days (if needed) */
protected function lastMonth($date) {
$this->prev = new DateTime($date . “-1 Month”);
$this->prev->modify(“last day of this month”);
$this->prevMonth = $this->prev->format(‘w’); // Numeric day of the week the last month’s day falls on:
for ($x = $this->prevMonth; $x >= 0; $x–) {
$this->calendar[] = ‘
}
}
/* Calendar days */
protected function currentMonth($date) {
$this->current = new DateTime($date);
$this->days = $this->current->format(‘t’); // Days in the current month:
for ($x = 1; $x <= $this->days; $x++) {
$this->calendar[] = ‘
}
}
/* Next month filler days (if needed) */
protected function nextMonth($date) {
$this->next = new DateTime($date . ‘+1 Month’);
$this->next->modify(‘first day of this Month’);
$this->nextMonth = $this->next->format(‘w’);
if ($this->nextMonth === 0) {
return \NULL;
}
for ($x = 1; $x <= (7 - $this->nextMonth); $x++) {
$this->calendar[] = ‘
}
}
protected function twoDimensional() {
/* Initialize all the variables to zero */
$num = 0;
$b = 0;
$c = 0;
/* Create a while loop in order to create the two-dimensional array */
while ($num < sizeof($this->calendar)) {
/* Values being put into the two-dimensional array */
$this->dispCalendar[$b][$c] = $this->calendar[$num];
$num += 1; // Increment this variable by one to avoid infinite loop:
$c += 1; // Inside portion of the two dimension array:
/* Check to see if number can be divided by 7 (seven days in a week) */
if (($num % 7) == 0) {
$b += 1; // Increment the outer portion of the array by one to get the rows of weeks:
$c = 0; // Reset back to zero (Remember Arrays are zero based!):
}
}
}
protected function form() {
/* Create a div box and an unordered list */
$this->theForm .= '<div class="row calDays clearfix"><ul>' . "\n";
/* Create heading for the calendar */
$this->theForm .= '<li class="calHeading"><a class="calHeaderText" href="#" onclick="return false">' . $this->current->format('F j, Y') . '</a></li>' . "\n";
/* Create days of the week heading */
for ($x = 0; $x <= 6; $x++) {
$this->theForm .= '<li class="day"><a href="#" onclick="return false">' . $this->alphaDay[$x] . '</a></li>' . "\n";
}
/* Create the days of the calendar using foreach loops */
foreach ($this->dispCalendar as $innerCalendar) {
foreach ($innerCalendar as $value) {
$this->theForm .= $value . "\n";
}
}
$this->theForm .= "</ul></div>\n";
}
protected function generateCalendar($date) {
$this->lastMonth($date); // put previous month days in array as filler (if needed):
$this->currentMonth($date); // put current momth days in same array:
$this->nextMonth($date); // put next month days in same array as filler (if neeeded):
$this->twoDimensional(); // create two-dimensional array:
$this->form(); // create calendar form:
return $this->theForm; // return finished calendar for to be output as HTML:
}
}
[/php]
and since this is Christmas the style.css file:
body {
background-color: #ffa;
padding: 0;
margin: 0;
}
.clearfix:before, .clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
.calDays {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
max-width: 418px;
padding: 0;
margin: 0;
}
ol, ul {
list-style: none;
}
.calHeading {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
background-color: #267e9f;
width: 100%;
max-width: 418px;
padding: 1.250rem;
text-align: center;
}
.calHeaderText {
cursor: default;
text-decoration: none;
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
font-size: 1.2rem;
color: #fff;
}
.day {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
float: left;
display: block;
background-color: #f3f3f3;
text-align: center;
width: 100%;
max-width: 54px;
height: 54px;
line-height: 54px;
}
.day a {
cursor: default;
text-decoration: none;
color: #000;
}
.calday {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
float: left;
display: block;
background-color: #e7d1a2;
text-align: center;
width: 100%;
max-width: 54px;
height: 54px;
}
.calday a {
pointer-events: none;
cursor: default;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: black;
font-size: 1.0rem;
line-height: 54px;
}
li.calday:nth-child(even), li.calday:nth-child(odd) {
border-right: 1px solid black;
border-top: 1px solid black;
}
li.calday:nth-child(7n+8) {
border-right: none;
}
li.highlightHoliday {
background-color: #cccdc8;
font-weight: bolder;
font-size: 1.0rem;
}
li.highlightHoliday a {
color: white;
}
a.highlightToday {
color: #12c712;
font-weight: bolder;
font-size: 1.2rem;
}
a.fade {
color: #aaa;
font-size: 1.0rem;
}
It’s free to use and modify, I hope this helps out someone. If you don’t use the calendar at least you can see how neat and compact OOP can be. I even plan to add abstract methods to this in the near future for even easier modifications and also put this on Github someday.