Del.Icio.Us Digg Technorati Reddit Stumble Facebook

Date Formatting

Posted on 20 Feb 2008 21:01:18 by John Slater in PHP

One of the first things i learnt from my PHP book was the mighty date function. It's very hard for me to remember the date function and how to format it correctly, which is why its nice to always have a reference to the formatting key, i like w3schools for that, but i want you guys to choose slater.john.

The date function is exactly what it says on the tin, the date. With the date function you can display the current date in a format you chose or just format a time stamp into a format you want.

In this tutorial i will first tell you how to show todays date in a nice format, then after that i will tell you how to format a time-stamp into a format you can read.

Before we get started you need to choose exactly how you want your date to be formatted. I find the best way to format todays date is 01/01/08, or in textual form it would be Day/Month/Year. With this information you need to look at the key below and use the key to decide exactly what you will use to format the date. (Case is very important, so make sure you use the correct letter-case.)

Day
d - Day of the month, from 01-31
D - A 3 letter text representation of the day, Mon, Tue etc...
j - The day of the month without the zero at the front, from 1-31
l - (lower-case L) A full text representation of the day, Monday, Tuesday etc...
S - Shows the day as a number followed by st, nd, rd or th. (best used with j)

Day - Other
w - Day of the week number, Sunday = 0, Monday = 1, Tuesday 2
z - The day (number) of the year, from 0 to 365
W - A number representation of the week of the year (weeks start Monday)

Month
m - Numeric representation of a month, from 01 to 12
n - Numeric representation of the month without the first zero, 1 to 12
M - Three letter text representation of the month, Jan, Feb, Mar etc...
F - Full text representation of the month, January, March etc...

Month - Other
t - The number of days in the month
L - Whether or not its a leap year, 1 for yes and 0 for no

Year
Y - A four digit number for the year, 2001, 1989, 1999 etc...
y - A two digit number for the year, 01, 89, 99 etc...

Did you know that the date function can also format time...

Time
a - Lowercase am or pm
A Uppercase AM or PM

Hour
g - 12-hour format number without first zero, 1 to 12
G - 24-hour format number without first zero, 0 to 23
h - 12-hour format of an hour with first zero, 01 to 12
H - 24-hour format of an hour with first zero, 00 to 23

Minutes & Seconds
i - Minutes with first zero, 00 to 59
s - Seconds with first zero, 00 to 59

Timezone
e - The timezone identifier, UCT, Atlantic/Azores
O - Different to GMT (UK) in hours, 0100+ etc...
T - Timezone three letter representation, GMT, EST etc...
Z - Timezone offset in seconds, -43200 to 43200

Right, now you have a list you can decide how you want to format it, for this tutorial we will just use Day/Month/Year.

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

this will output...

20/02/08

You can mix it up now you have the basics, lets make our date look like this "Today is Wednesday the 20th of February 2008". The code is pretty simple...

echo 'Today is '.date("l").' the '.date("jS").' of '.date("F").' '.date("Y").'';

Demo of the above

Now that you know to format for the date for this moment in time lets try and format a date from a time-stamp. It uses the exact same method with just the one difference. For the people who don't know what a time-stamp is it's basically the number of seconds after the Unix Epoch on January 1st 1970. For this tutorial we will use the time stamp from the 13th March 1989, the stamp looks like this... 605768400.

Now we have this information lets start with the code...

<?
$stamp = ' 605768400';
echo date("d/m/y", $stamp);
?>

As you can see the only difference in the date function is that now we are using the stamp as the date to reference, as you can see i set a variable called $stamp which contained the Unix Time-stamp, this just cleans things up.

I hope that you can take away a lot about dates from this tutorial, they are very useful for website creation, where possible i suggest using a time-stamp and just reformatting it using PHP and the date function, makes everything more flexible.

To find the unix time-stamp for any date use the time-stamp website here.


Share This Story:
digg del.icio.us Technorati reddit furl stubleupon Facebook