5.3. Code and Code Explanation

I will jump right into this application by first discussing how to generate a month-view calendar. Afterwards I will cover the personal calendar application as a whole and discuss the day-view calendar, form and events list. Then I will discuss the reminder and export convenience features.

5.3.1. Creating a Month-View Calendar

The code to generate a calendar that displays days within a month consists really of nothing more than a couple loops once information about the desired month has been established.

A unix timestamp is retrieved first either from a URL parameter or from the time() function, which returns the current timestamp. When the page is initially requested there is little likelihood that a timestamp will be sent in the URL so the current date will be the default. Afterwards a specific timestamp can be passed from links as other dates are navigated. The date() function can then use the timestamp value to identify important aspects of the date. It is a pretty powerful function that can do more than just format a timestamp for display; it can be used to determine the starting day of a month, the number of days in a month, whether the year is a leap year or not, and so on.

$timestamp = (isset($_GET['t'])) ? $_GET['t'] : time();

list($month, $day, $year) = explode('/', date('m/d/Y', $timestamp));
$first_day_of_month = date('w', mktime(0, 0, 0, $month, 1, $year));
$total_days = date('t', $timestamp);

Tables 5-1 and 5-2 show the available format ...

Get PHP and MySQL®: Create-Modify-Reuse now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.