Introduction
Today we are going to build a functional calender using PHP.
About a year ago I was given the task of implementing an
events calender on a website. The customer was not a fan of
javascript or ajax, and insisted on a PHP-only
implementation. That made things rather interesting. Gone
were all my dreams of a quick implementation with a jQuery
datepicker. Gone were any thoughts of simplicity whatsoever,
or so I thought. Then it struck me. What was a calender but
just a looping number system? The more I thought about it
the more simple it became. At the end of the day, with a
little research into PHP’s date functions I had come up with
something that actually worked. The working example can be
downloaded here.
About a year ago I was given the task of implementing an
events calender on a website. The customer was not a fan of
javascript or ajax, and insisted on a PHP-only
implementation. That made things rather interesting. Gone
were all my dreams of a quick implementation with a jQuery
datepicker. Gone were any thoughts of simplicity whatsoever,
or so I thought. Then it struck me. What was a calender but
just a looping number system? The more I thought about it
the more simple it became. At the end of the day, with a
little research into PHP’s date functions I had come up with
something that actually worked. The working example can be
downloaded here.
We begin by setting a few environmental variables. Obviously
we need to know the URL to our script, and since we are
dealing with a calender we also need to know things about
the date we are at. What we will be doing here is checking
if a variable is set in the URL, telling us what year, month
and day we are looking for, otherwise we will just default
to this year, this month and today.
we need to know the URL to our script, and since we are
dealing with a calender we also need to know things about
the date we are at. What we will be doing here is checking
if a variable is set in the URL, telling us what year, month
and day we are looking for, otherwise we will just default
to this year, this month and today.
// set the script location
$self = $_SERVER['PHP_SELF'];
// check if a month variable has been set in the url, else use PHP's date() function to set the current month.
if(isset($_GET['month'])):
$month = $_GET['month'];
elseif(isset($_GET['viewmonth'])):
$month = $_GET['viewmonth'];
else: $month = date('m');
endif;
Now we check if a year variable is set in the URL, else use
PHP’s
if the current year is not set in the URL.
PHP’s
date() function
to set the current yearif the current year is not set in the URL.
if(isset($_GET['year'])):
$year = $_GET['year'];
elseif(isset($_GET['viewyear'])):
$year = $_GET['viewyear'];
else: $year = date('Y');
endif;