Home arrow PHP arrow Page 2 - Miles To Go Before I Sleep...

Building The Foundation - PHP

Ever wished you had an appointment calendar you could accessthrough your Web browser? Well, it's not too late. This week, learn how tobuild a simple PHP-based appointment calendar which allows you to view,add, edit and delete appointments and meetings. And if you're just gettinginto PHP, this article will show you how to apply the theory you've spentso much time learning to a real-world application.

  1. Miles To Go Before I Sleep...
  2. Building The Foundation
  3. Seven Days, Seven Nights
  4. Bringing In The Database
  5. Adding, Editing, Deleting...
  6. The Final Touch
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
February 05, 2001

print this article


The calendar we're going to build should have both a "month view" and a "day view" - selecting a specific day from the month view should display scheduled appointments for that day. Options should be available to add new appointments to the list, or edit and delete existing appointments.

This is a good time for you to download the source code, so that you can refer to it throughout this tutorial (you will need a Web server capable of running PHP and a mySQL database in order to run the application).


We'll begin with the month view, a file I'm going to call "month.view.php". The first thing to do is seta few variables which decide the month and year to be displayed - these variables will be used throughout the application, and are crucial to it functioning correctly. PHP's date() function is perfect for obtaining this information:

<? // set up some variables to identify the month, date and year to display if(!$currYear) { $currYear = date("Y"); } if(!$currMonth) { $currMonth = date("n"); } if(!$currDay) { $currDay = date("j"); } ?>

Thus, the variables $currDay, $currMonth and $currYear will hold the values corresponding to the current date, month and year respectively. For example, on 25 January 2001, the variables would look like this:

<? $currYear = 2001; $currMonth = 1; $currDay = 25; ?>
The date() function comes with numerous modifiers which allow you to extract just those specific segments of the date and time you need from a standard UNIX timestamp. This is a feature I'll be using a lot as I begin building my application, so if you're not familiar with it, take a look at the PHP4 date() function reference at before reading further.

Next, it's time to set up some friendly names for the various months of the year and days of the week - these will come in handy when displaying the calendar.

<? // list of names for days and months $days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); // number of days in each month $totalDays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // if leap year, modify $totaldays array appropriately if (date("L", mktime(0,0,0,$currMonth,1,$currYear))) { $totalDays[2] = 29; } ?>
Yup, the date() function even lets you find out if the year under consideration is a leap year - if it is, it's necessary to modify the $totalDays array for the month of February. Since the date() function only works on a correctly-formatted UNIX timestamp, the mktime() function is used to first convert the numeric month and year into an acceptable format.

Before getting into the calendar display, there's one more thing needed: the day of the week on which the first of the month falls.

<? // find out which day the first of the month falls on $firstDayOfMonth = date("w", mktime(0,0,0,$currMonth,1,$currYear)); ?>
The first day of the month (from the $firstDayOfMonth variable) and the last day (from the $totalDays array) provide the bounding values for the month view I'm going to be building.

This article copyright Melonfire 2001. All rights reserved.

>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: