Creating A Simple Calendar in PHP

In this first of 4 tutorials, we build a simple, but working calendar for display purposes, looking at the techniques involved.

In this tutorial, we look at creating calendars in PHP. In this first of 4 tutorials, we build a simple, but working calendar for display purposes, looking at the techniques involved. In the second part we'll going to build a more advanced calendar that can display events for each day and has customised styling.

Advertisement DMXzone Calendar

 The DMXzone Calendar extension enables you to add a great looking calendar with many different styles and effects on your sites.  Enrich any form used for events, appointments or birthdays. Use it as a date picker or inline calendar with its unique design and in flight animation options. Embed multiple calendars, select date ranges, and limit the selection to specific dates.

Overview

Table of Content:

  • Creating Calendars in PHP
  • The PHP Date() Command
    • Overview
    • This returns the date in the format Sunday, 12th October 2003 for example
    • Useful tokens
  • Building the Calendar
    • Displaying the current month
      • Creating the HTML
      • Creating the PHP
      • Formatting Tip
      • The Calendar Output
  • Summary

In this tutorial, we're going to look at creating calendars in PHP. In this first part of the series we build a simple calendar for display purposes, looking at the techniques involved. In the second part we'll build a more advanced calendar that can display events for each day for example and has customised styling.

As well as being useful, calendars use a number of different programming techniques, which can be utilised in many other areas. We'll also look at the best ways to work with times and dates, and how to findthe number of days in a month, or what weekday a date fall on for example.

The most important command when building calendars is the PHP date() command, so let's look at this first.

The PHP Date() Command

Overview

The PHP date() command is incredibly useful, due to the different data that it can return. Although we'll look at some of the functions here, for a full overview of the command its well worth checking out the command in the online PHP manual at http://www.php.net/manual/en/function.date.php

The format for the date() command is:

date($dateFormat, (optional timestamp))

$dateFormat contains a string containing tokens which tell the date command which information to return. Optional timestamp is a UNIX timestamp which represents the date that you want to work with; if it's not given then the date command will automatically work with the current date. As well as working with dates, the date() command can also work with times.

As an example of how the date() command is used, we'll look at a few examples:

<?php echo date("m/d/Y"); ?>

This returns the current date in the format mm/dd/yyyy e.g. 10/12/2003

<?php echo date("l, jS F Y"); ?>

This returns the date in the format Sunday, 12th October 2003 for example

<?php echo date("G:i"); ?>

This returns the time in the format 19:06 for example

<?php echo date("h:i a"); ?>

This returns the time in the format 7:06 am

You can see that the date() command is extremely versatile, and allows you to create times and dates in many different formats. It can also return other useful information such as the number of days in the current month, the current day of the week, whether it's a leap year etc. All of these come in extremely useful when creating a calendar, and saves a lot of manual coding.

Now let's look at some of the tokens that we are going to be using.

Useful tokens

Because there are over 30 tokens available, we won't look at them all here. Instead, we'll look at a selection of the most useful tokens, and the ones were going to use to build our calendar. To see a complete list of all the tokens (which is well worth printing out for reference), see the online PHP manual at: http://www.php.net/manual/en/function.date.php

Token

Description

Example of Output

d

Day of the month, 2 digits with leading zeros

01 - 31

D

3 Letters representing the day

Mon – Sun

F

Month Name

January – December

h

Hour with leading zeros

01 – 12

H

Hour in 24 Hour Format

00 – 23

i

Minutes

00 – 59

j

Day

1 – 31

l

Day of the week

Sunday – Saturday

L

Whether it's a Leap Year

0 = no, 1 = yes

m

Month Number

1 – 12

M

Month Name

Jan – Dec

t

Number of days in month

28 - 31

w

Number representing day of week

0 = Sunday – 6 = Saturday

W

ISO-8601 Week Number (weeks start on Monday)

E.g. 34

y

Year (2 digits)

99 or 03

Y

Year (4 digits)

1999 or 2003

z

Number representing Day of the Year

0 - 366

Table 1 – Useful date tokens

Note that the case of the tokens is important, and a lowercase letter and the same uppercase letter can represent two different formats. You can also have several different tokens in the same string.

If you include any characters in the token string that aren't actually tokens, these tokens will be left unchanged so you can use them for adding your own formatting to the output. For example, if you wanted the current date in the MySQL date format (yyyy-mm-dd) then you could use:

<?php echo date("Y-m-d"); ?>

This would output 2003-12-10 for example, and you can see that the hyphens (-) have been left unchanged and are included in the output. Any non token characters can be used e.g.

<?php echo date("m/d/Y"); ?>

would output 10/12/2003 for example.

Now that we've looked at how the date() command is used, and at the tokens we have available we can move on and start looking at building a calendar.

Gareth Downes-Powell

Gareth Downes-PowellGareth has a range of skills, covering many computer and internet related subjects. He is proficient in many different languages including ASP and PHP, and is responsible for the setup and maintenance of both Windows and Linux servers on a daily basis.


In his daily web development work he uses the complete range of Macromedia software, including Dreamweaver MX, Flash MX, Fireworks MX and Director to build a number of websites and applications. Gareth has a close relationship with Macromedia, and as a member of Team Macromedia Dreamweaver, he has worked closely in the development of Dreamweaver, and was a beta tester for Dreamweaver MX.


On a daily basis he provides support for users in the Macromedia forums, answering questions and providing help on a range of different web related subjects. He has also written a number of free and commercial extensions for Dreamweaver MX, to further extend its capabilities using its native JavaScript API’s or C++.


As a web host, Gareth has worked with a range of different servers and operating systems, with the Linux OS as his personal favourite. Most of his development work is done using a combination of Linux, Apache and MySQL and he has written extensively about setting up this type of system, and also running Apache and MySQL under Windows.

See All Postings From Gareth Downes-Powell >>

Reviews

Revised Version ?

June 6, 2008 by John Ashton

Having used this great tutorial some years ago when it first appeared I was keen to learn about the revised version and the methods used to interact with a database. So, I purchased the Tut, downloaded then read it only to find that this is the same as I previously had.

So how do you intend to provide what was advertised, "Creating a simple calendar in PHP (updated) ???

Not happy guys !!

Advanced Calendars

October 5, 2018 by Martin Cotterill
"In the next part of this tutorial, we will look at styling the calendar using CSS, and making more advanced calendars that can interact with a database." Is this ready yet?

RE: Advanced Calendars

October 5, 2018 by Teodor Kuduschiev
Hello Martin, this article is more than 10 years old! Better check our newer articles and extensions.

You must me logged in to write a review.