Over the weekend I developed a dynamic calendar using php for I came across a thread on the internet where a person was having trouble creating their own. I thought to myself that this would be challenging, so I took it upon myself if I could write my own script. I did and it works pretty darn good. I have future plans to have it where a person can make a notation on a certain date to remember an appointment or remember an event. I also plan on adding JavaScript (jQuery) and Ajax into the mix to make it spiffier and where the page doesn’t have to reload. However, I thought I would share some of the code here for people who want to tackle the same thing. It would probably benefit you if you know OOP, but I think you can figure it out how to do it the procedural way if you don’t know OOP. As I side note I even do some of the code in the procedural way.
Here is the main script:
[php]<?php
require(‘lib/includes/utilities.inc.php’);
$monthlyCalendar = array();
$displayCalendar = array();
$myDate = (isset($_POST[‘myDate’])) ? date(‘F Y’, strtotime($_POST[‘myDate’])) : date(‘F Y’);
$month = new MyCalendar();
$displayDate = MyCalendar::dateHumanForm($myDate);
/* This is the important one for it figures out the number of days in the current month */
$daysInMonth = MyCalendar::daysInMonth($myDate);
$displayMonth = MyCalendar::displayMonth($myDate);
$displayDay = MyCalendar::displayDay($myDate); // Day of the Week:
$displayYear = MyCalendar::displayYear($myDate);
$prevMonth = MyCalendar::numericDay($myDate);
$futureMonth = 6 - MyCalendar::numericDay($displayMonth . ’ ’ . $daysInMonth . ', ’ . $displayYear);
/* Calculate the previous number of days from previous month for filler and /
/ day’s location. Then put them in an array /
for ( $y = $prevMonth ; $y > 0; $y-- ) {
$monthlyCalendar[] = ‘’ . MyCalendar::previousMonth($y) . ‘ ’;
}
/ Since we know the last location of previous month’s day location in the week, /
/ we don’t have to figure out the location for the current month, we just have /
/ to add it on to the existing array. /
for ( $x = 1; $x <= $daysInMonth; $x++ ) {
$monthlyCalendar[] = ‘’ . $x . ‘ ’;
}
/ If there are any blank days at the end of calendar then put days from next /
/ month into the existing array. This should complete this array. */
for ( $a = 1; $a <= $futureMonth; $a++ ) {
$monthlyCalendar[] = ‘
}
/* I figure it would be easier to create the calendar using a two-dimensional /
/ array rather than a single-dimensional array. I’m using a old fashion table /
/ I figured it would be easier to style in css and it was. There is nothing /
/ wrong with using tables as long as you aren’t designing the whole website /
/ with tables. The following code I converted the single-dimensional array to /
/ a two-dimensional array. */
/* Initiaze 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($monthlyCalendar) ) {
/* Valuse being put into the two-dimensional array */
$displayCalendar[$b][$c] = $monthlyCalendar[$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!)
}
}
include(‘lib/html/header.php’);
?>
<?= $displayMonth . ' ' . $displayYear; ?> |
---|
' . MyCalendar::retrieveDays($d) . ' | ' .PHP_EOL; } ?>
[/php]
Here is the class that goes along with it:
[php]<?php
class MyCalendar {
protected $monthArray = array();
protected static $day = array( 0 => “Sun”,
1 => “Mon”,
2 => “Tue”,
3 => “Wed”,
4 => “Thu”,
5 => “Fri”,
6 => “Sat”
);
protected $months = array( 0 => “January”,
1 => “February”,
2 => “March”,
3 => “April”,
4 => “May”,
5 => “June”,
6 => “July”,
7 => “August”,
8 => “September”,
9 => “October”,
10 => “November”,
11 => “December”
);
protected static $daysInMonth; // Total Days in a Month:
protected static $mysqlFormat;
protected static $displayMonth;
protected static $displayDay;
protected static $displayYear;
protected static $numericMonth; // 1 through 12 Months:
protected static $numericDay; // Numeric Day of the Week (0 - 6):
protected static $displayDate;
protected static $previousMonth;
protected static $futureMonth;
public static function retrieveDays($days) {
return self::$day[$days];
}
public static function numericMonth($date) {
return self::$numericMonth = date('n', strtotime($date));
}
/* Numeric representatin of the day of the week */
public static function numericDay($date) {
return self::$numericDay = date('w', strtotime($date));
}
public static function daysInMonth($date) {
return self::$daysInMonth = date('t', strtotime($date));
}
public static function mysqlFormat($date) {
return self::$mysqlFormat = date("Y-m-d H:i:s", strtotime($date));
}
public static function dateHumanForm($date) {
return self::$displayDate = date("F j, Y", strtotime($date));
}
public static function displayMonth($date) {
return self::$displayMonth = date('F', strtotime($date));
}
public static function displayDay($date) {
return self::$displayDay = date('l', strtotime($date));
}
public static function displayYear($date) {
return self::$displayYear = date('Y', strtotime($date));
}
public static function previousMonth($num) {
return self::$previousMonth = date('j', strtotime(self::$displayDate . ' ' . $num . ' days ago'));
}
public static function futureMonth($num) {
return self::$futureMonth = date('j', strtotime(self::$displayMonth . ' ' . self::$daysInMonth . ', ' . self::$displayYear . ' +' . $num . ' days'));
}
}[/php]
Like I said it would be helpful to know OOP, but I think it could be easily converted over to procedural style like I said.
Enjoy…