[ Team LiB ] Previous Section Next Section

Converting a Timestamp with getdate()

Now that you have a timestamp to work with, you must convert it before you present it to the user. getdate() optionally accepts a timestamp and returns an associative array containing information about the date. If you omit the timestamp, it works with the current timestamp as returned by time(). Table 16.1 lists the elements contained in the array returned by getdate().

Table 16.1. The Associative Array Returned by getdate()

Key

Description

Example

seconds

Seconds past the minute (059)

28

minutes

Minutes past the hour (059)

7

hours

Hours of the day (023)

12

mday

Day of the month (131)

20

wday

Day of the week (06)

4

mon

Month of the year (112)

1

year

Year (four digits)

2004

Yday

Day of year (0365)

19

weekday

Day of the week (name)

Thursday

month

Month of the year (name)

January

0

Timestamp

948370048

Listing 16.1 uses getdate() (line 11) to extract information from a timestamp, using a foreach statement to print each element (line 12). You can see a typical output example in Figure 16.1. getdate() returns the date according to the local time zone.

Figure 16.1. Using getdate().

graphics/16fig01.gif

Listing 16.1 Acquiring Date Information with getdate()
 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.1 Acquiring Date Information with getdate()</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: $date_array = getdate(); // no argument passed so today's date will be used
12: foreach ( $date_array as $key => $val ) {
13:   print "$key = $val<br/>";
14: }
15: ?>
16: <hr/>
17: <p>
18: <?
19: print "Today's date: ";
20: print $date_array['mon']."/".$date_array['mday']."/".$date_array['year'];
21: ?>
22: </p>
23: </div>
24: </body>
25: </html>
    [ Team LiB ] Previous Section Next Section