More Free Resources

Hello again. Today's tutorial is separated into two parts. In the first part we will get familiar with date and time formatting and in the second we will exercise date and time manipulations. Probably most of you are familiar with formatting, but for those who are not and for those that would like to remind of it please read below.

You may think that date and time topic is not very important in web applications and you'll be wrong. Almost every one has it. It may be not so obvious, it may be even hidden from the regular users, but it is there. Just think about it for a second. Where do we always use date and time? ... If you still haven't got the answer here it is. The most common place for dates and time are databases and log files. Why? We all want to have a log of users' actions. We log everything so we can keep track of what happens, because one day it will inevitably go wrong and we would like to now what caused the problem. The main part of every log is "when it happened". It may be only the administrator of the web application, who is reading it, or it can be hundreds of company's employees that carefully sort every event that took part, but they all need to know "date & time". You may still ask yourself why I started with User Interface but take a look at this:

1202395558

Not many people can read this clearly without any problems. This is the current server time. Now see this:

2008-02-07 16:45:58
Feb 07 08 4:45:58 pm

For those of you that are not genius enjoying themselves by calculating big integer time, I will reveal the mystery of how we can format dates and time. We will begin with dates formatting. It is done using date() function. It accepts two parameters. Format string and time as integer. We will use time() for current time integer. Let's see some examples.

echo date("d-m-y", time()); // 07-02-08

The dash between the characters in the format string above is exactly what the output is going to be separated with.

echo date("D j/n/Y", time()); // Thu 7/2/2008

The dash here is replaced with forward slash.

echo date("jS of F Y", time()); // 7th of February 2008

We can use any character we want here, just keep in mind that if it has a special meaning it has to be escaped. And just to be sure that there are no mistakes I suggest that you always escape every character you use.

echo date("d M y", time()); //07 Feb 08

Formatting the date using short representation for month names and short year.

echo date("l jS of F", time()); // Thursday 7th of February

Full day and month name with ordinal suffix for the day of the month.

Date format characters' legend by examples:

d - Numeric representation of a day, with leading zeros 01 through 31.
m - Numeric representation of a month, with leading zeros 01 through 12.
y - Numeric representation of a year, two digits.

D - Textual representation of a day, three letters Mon through Sun.
j - Numeric representation of a day, without leading zeros 1 through 31.
n - Numeric representation of a month, without leading zeros 1 through 12.
Y - Numeric representation of a year, four digits.

S - English ordinal suffix for the day of the month. Consist of 2 characters st, nd, rd or th.
F - Textual representation of a month, January through December.

M - Textual representation of a month, three letters Jan through Dec.

l textual representation of the day of the week Sunday through Saturday.
Now we move on to our next task which is time formatting. We are still going to use the same date() function, but with different format characters.

echo date("G:i:s", time()); //16:45:58

Working with time is a little bit simpler, because there is only one way to represent minutes and seconds.

echo date("H:i:s", time()); //16:45:58

Despite of the very few options for minutes and seconds formats we have a lot of hour formatting styles. This one represents hours with leading zeroes 0 through 23.

echo date("g:i a.", time()); //4:45 pm.

This is an example of time format with lowercase ante meridiem and post meridiem (am/pm).

echo date("h:i A.", time()); //04:45 PM.

This is an example of time format with uppercase AM/PM.
Time format characters' legend by examples:

G 24-hour format of an hour without leading zeros 0 through 23.
i Numeric representation of minutes with leading zeros 00 through 59.
s Numeric representation of seconds with leading zeros 00 through 59.
H 24-hour format of an hour with leading zeros 00 through 23.
a Lowercase Ante meridiem and Post meridiem am or pm.
g 12-hour format of an hour without leading zeros 1 through 12.
A Uppercase Ante meridiem and Post meridiem AM or PM.
h 12-hour format of an hour with leading zeros 01 through 12.
And when we combine both date and time formatting we get some really nice looking output strings.