Converting to a string

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 use the current time.

Parameter one is the key: it is a string of letters from a predefined list of options. 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, don't 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.

Line three is very simple - it just prints the current year. But on line four 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 five 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 six 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.

 

Next chapter: Converting from components >>

Previous chapter: Converting from a string

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar