Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Working with Date and Time Wish List | Report Bug | About Me ]

4.5.3     Converting to a string: date()

This is NOT the latest copy of this book; click here for the latest version.

string date ( string format [, int timestamp])

However easy Unix timestamps are in scripts, they are not much good to end users - few people track their dates using numbers of seconds!

As mentioned already, users like to have their dates in all sorts of formats, so PHP gives you the choice to convert timestamps into strings in a number of ways using the date() function.

Date() takes two parameters, with the second one being optional as with strtotime(). Parameter one is a special string containing formatting codes for how you want the timestamp converted. Parameter two is the timestamp you want to convert - if you do not supply it, PHP assumes you want to convert the current timestamp.

Parameter one is the key - it is a string of letters from a predefined list of 31 possibles. You can use other characters in the string, and these are copied directly into the formatted date. If you are trying to put words into the date format that you do not want to be converted into their date equivalent, you need to escape them with a backslash \ . To make things even more confusing, if your escaped letter is an existing escape sequence, then you need to escape it again! If that sounds confusing, do not worry - we will be looking at examples.

Here is the complete list of date format characters. Note that they are case sensitive!

Format character

Description

Example

a

Lowercase am/pm

am or pm

A

Uppercase am/pm

AM or PM

B

Swatch Internet Time

000 to 999

c

ISO 8601 date, time, and time zone

2004-06-18T09:26:55+01:00

d

2-digit day of month, leading zeros

01 to 31

D

Day string, three letters

Mon, Thu, Sat

F

Month string, full

January, August

g

12-hour clock hour, no leading zeros

1 to 12

G

24-hour clock hour, no leading zeros

0 to 23

h

12-hour clock hour, leading zeros

01 to 12

H

24-hour clock hour, leading zeros

00 to 23

i

Minutes with leading zeros

00 to 59

I

Is daylight savings time active?

1 if yes, 0 if no

j

Day of month, no leading zeros

1 to 31

l

Day string, full

Monday, Saturday

L

Is it a leap year?

1 if yes, 0 if no

m

Numeric month, leading zeros

01 to 12

M

Short month string

Jan, Aug

n

Numeric month, no leading zeros

1 to 12

O

Difference from GMT

200

r

RFC-822 formatted date

Sat, 22 Dec 1979 17:30 +0000

s

Seconds, with leading zeros

00 to 59

S

English ordinal suffix for day number

st, nd, rd, or th

t

Number of days in month

28 to 31

T

Time zone for server

GMT, CET, EST

U

Unix Timestamp

1056150334

w

Numeric day of week

0 (Sunday), 6 (Saturday)

W

ISO-8601 week number of year

30 (30th week of the year)

y

Two-digit representation of year

97, 02

Y

Four-digit representation of year

1997, 2002

z

Day of year

0 to 366

Z

Time zone offset in seconds

-43200 to 43200

As you can see, there is lots of choice available to you when converting from a timestamp to dates. Here are some examples of the format characters in use:

<?php
    
print date("H:i") . "\n";
    print
"The day yesterday was " . date("l", time() - 86400) . "\n";
    print
"The year is " . date("Y") . "\n";
    print
date("jS of F Y") . "\n";
    print
"My birthday is on a " . date("l", strtotime("22 Dec 2004")) . " this year.\n";
    print
date("\M\y b\i\\r\\t\h\d\a\y \i\s o\\n \a l \\t\h\i\s \ye\a\\r. ", strtotime("22 Dec 2004")) . "\n";
?>

The first line is the most basic, and prints out the current time in 24-hour clock format. The second line is slightly more complicated mixing in the output of date() with a text string to get a natural-looking statement.

On line three we have a more complicated example, which outputs the date in the format of "22nd of October 2003". Notice that we have the word "of" in there amongst the date format and it has been seamlessly passed through to the output instead of being converted. The reason for this is that lowercase O and lowercase F do not have any formatting purpose in the date function (although this may be changed in the future) so they are just copied straight into output.

On line four you can see we have our date function embedded between two other strings, which makes for particularly nice-looking output. It is possible to embed the strings inside the date format, but it ends up looking like line five!

In line five you can see that the same strings from line four are now inside the date format. Most letters in there function as date format symbols so they have been escaped: \M\y, etc. Lowercase B, lowercase O, and lowercase E all have no date format meaning, so they are not escaped. Lowercase R, lowercase T, and lowercase N all have special meanings as escape sequences themselves (carriage return, tab, and new line), which means they need to be double escaped. Notice that each of the escape sequences has two backslashes before them rather than one - that is the key. One backslash stops PHP from reading them as date format characters, and the other one is to stop PHP reading them as escape sequences.

There is a lot you can do with dates, so I strongly recommend that you just play around with different formats to see what you can do with different combinations.





<< 4.5.2 Converting from a string: strtotime()   4.5.4 Converting from components: mktime() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 07 Sep 2008

weeeeee

A PHP User - 07 Sep 2008

you have to set it to refresh otherwise people have to hit refresh to see the new time, doing time is better done in javascript as it can automatically update, and the date and itme functions are better in javascript.

~!~ DoctoR JackaL ~!~ - 07 Sep 2008

Thx alot for this boook , it really teaches PHP , even though some thingZ are not explained , anywayz:
i just wanted 2 ask , cant the time be automatically refreshed ?

thx , bye

vlur - 07 Sep 2008

since PHP 5.1.0 lowercase e serves as a Timezone identifier so you'd have to escape this letter too.

stellaris@h3c.de - 07 Sep 2008

Mike,

thanks a million for pointing this out, I was really confused about the line

print "The day yesterday was " . date("l", time() - 86400) . "\n";

(I didn't notice it in the table the first time around!) and wondered what the "1" was supposed to do there as a format option.

A PHP User - 07 Sep 2008

UMM... your line numbers are mixed up, you skipped the true line 3. There are 6 lines and you only covered 5.

mflintjer@charter.net - 07 Sep 2008

Hey there, my name is Mike and have figured it out, but other readers may not be intuitive enough to understand.

In the table above, and throughout the book, you have used Verdana or Arial as your font. In 2 sections of the table capital "i" and lower-case "L" look VERY similar. I copied each of the sections, and pasted them in the comment box, and saw the difference.

Maybe you could place the words capital and lower-case next to the respective letters to distinguish them apart. I also understand that the table is set up in alphabetical order, but as I pointed out, the 2 letters are VERY similar to each other and may cause headachesfor other users.

Just an idea.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow