[ Team LiB ] Previous Section Next Section

Converting a Timestamp with date()

You can use getdate() when you want to work with the elements it outputs. Sometimes, though, you only want to display the date as a string. The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument you must pass to it. In addition to the format string, date() optionally accepts a timestamp. Table 16.2 lists the codes a format string can contain; any other data you include in the format string passed to date() is included in the return value.

Table 16.2. Format Codes for Use with date()

Format

Description

Example

a

am or pm lowercase

pm

A

AM or PM uppercase

PM

B

Swatch beat (timezone-free 'Internet time')

771

d

Day of month (number with leading zeroes)

08

D

Day of week (three letters)

Wed

F

Month name

October

g

Hour (12-hour format—no leading zeroes)

6

G

Hour (24-hour format—no leading zeroes)

18

h

Hour (12-hour format—leading zeroes)

06

H

Hour (24-hour format—leading zeroes)

18

i

Minutes

31

I

Daylight savings time (Boolean value)

1

j

Day of the month (no leading zeroes)

8

l

Day of the week (name)

Wednesday

L

Leap year (1 for yes, 0 for no)

0

m

Month of year (number—leading zeroes)

10

M

Month of year (three letters)

Oct

n

Month of year (number—no leading zeroes)

10

o

Offset in hours from GMT (in [+-]HHMM format)

+0100

r

Full date standardized to RFC 822 (http://www.faqs.org/rfcs/rfc822.html)

Wed, 8 Oct 2003 18:31:15+0100

s

Seconds, with leading zeroes

15

S

English suffix for date in month (e.g. 20th)

th

t

Number of days in the given month

31

T

Timezone setting on the machine used

BST

U

Unix timestamp

1065634275

w

Day of week (number indexed from Sunday = 0)

3

W

Week of year

41

y

Year (two digits)

03

Y

Year (four digits)

2003

z

Day of year (0–366)

280

Z

Offset in seconds from GMT

3600

Listing 16.2 puts a few format codes to the test.

Listing 16.2 Formatting a Date with date()
 1: <!DOCTYPE html PUBLIC
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 16.2 Formatting a Date with date()</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: print date("m/d/y G.i:s", time());
12: // 10/08/03 19.17:42
13: print "<br/>";
14: print "Today is ";
15: print date("jS of F Y, \a\\t g.i a", time());
16: // Today is 8th of October 2003, at 7.17 pm
17: ?>
18: </div>
19: </body>
20: </html>

In Listing 16.2 we call date() twice—the first time on line 11 to output an abbreviated date format, the second on line 15 for a longer format.

Although the format string looks arcane, it is easy to build. If you want to add a string to the format containing letters that are format codes, you can escape them by placing a backslash (\) in front of them. For characters that become control characters when escaped, you must escape the backslash that precedes them. "\n" should become "\\n", for example, if you want to include an n in the format string. date() returns information according to your local time zone. If you want to format a date in GMT, you should use the gmdate() function, which works in exactly the same way.

    [ Team LiB ] Previous Section Next Section