Specifications

Advanced PHP Techniques
P
ART IV
392
In this chapter, well discuss checking and formatting the date and time and converting
between date formats. This is especially important when converting between MySQL and PHP
date formats, UNIX and PHP date formats, and dates entered by the user in an HTML form.
Well cover
Getting the date and time in PHP
Converting between PHP and MySQL date formats
Calculating dates
Using the calendar functions
Getting the Date and Time from PHP
Way back in Chapter 1, PHP Crash Course,we talked about using the date() function to get
and format the date and time from PHP. Well talk about it and some of PHPs other date and
time functions in a little more detail now.
Using the date() Function
As you might recall, the date() function takes two parameters, one of them optional. The first
one is a format string, and the second, optional one is a UNIX time stamp. If you dont specify
a time stamp, then date() will default to the current date and time. It returns a formatted string
representing the appropriate date.
A typical call to the date function could be
echo date(“jS F Y”);
This will produce a date of the format 27
th
August 2000.
The format codes accepted by date are listed in Table 18.1.
T
ABLE 18.1 Format Codes for PHPs date() Function
Code Description
a Morning or afternoon, represented as two lowercase characters, either
amor pm.
A Morning or afternoon, represented as two uppercase characters, either
AMor PM.
B Swatch Internet time, a universal time scheme. More information is avail-
able at http://swatch.com/internettime/internettime.php3.
d Day of the month as a 2-digit number with a leading zero. Range is from
01to 31.
23 7842 CH18 3/6/01 3:43 PM Page 392