Page 518 - Beginning PHP 5.3
P. 518
Part III: Using PHP in Practice
Character Description
Z The time zone offset from GMT, in seconds. For example, the offset in
seconds for the “ America/Indiana/Indianapolis ” time zone is – 18000 ,
because – 5 hours x 60 minutes x 60 seconds = – 18,000 seconds
I (capital “ I ” 1 if the currently set time zone is in daylight saving time; 0 otherwise
)
Note that the time zone formatting characters deal with the script ’ s time zone, because the timestamp is
always in UTC. Usually the script ’ s time zone is set by the date.timezone directive in the php.ini file,
but you can use PHP ’ s date_default_timezone_set() function to change the time zone within your
script, if necessary.
As well as the separate date and time formatting characters just mentioned, date() gives you three
more formatting characters that return the date and time in one go:
Character Description
c The date and time as an ISO 8601 date. For example, 2006 - 03 - 28T19:42:00+11:00
represents March 28, 2006 at 7:42 in the evening, in a time zone that is 11 hours
ahead of GMT
r The date and time as an RFC 2822 date. For example, Tue, 28 Mar 2006
19:42:00 +1100 represents March 28, 2006 at 7:42 in the evening, in a time zone
that is 11 hours ahead of GMT. RFC 2822 dates are commonly used in Internet
protocols such as Web and email
U The timestamp that was passed to date() , or the timestamp representing the
current time if no timestamp was passed
For example, you could format a date and time in a nice, easy - to - read fashion like this:
$d = strtotime( “March 28, 2006 9:42am” );
// Displays “The 28th of March, 2006, at 9:42 AM”
echo date( “\T\h\e jS \o\\f F, Y, \a\\t g:i A”, $d );
Notice that non - formatting characters in the format string need to be escaped with a backslash, and
some special characters — like \f for the form feed character and \t for the tab character — need an
additional backslash to escape them.
date() converts the UTC timestamp supplied to your server ’ s time zone. If you ’ d rather keep the date
in UTC, use gmdate() instead:
// Set the current time zone to 5 hours behind GMT
date_default_timezone_set( “America/Indiana/Indianapolis” );
// Set $d to the timestamp representing March 28, 2006 2:42 PM UTC
480
9/21/09 9:15:30 AM
c16.indd 480 9/21/09 9:15:30 AM
c16.indd 480