13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

476 Chapter 21 Managing the Date <strong>and</strong> Time<br />

It is important to note that whenever it says st<strong>and</strong>ard format in Table 21.3, the formatting<br />

code gets replaced by the associated value according to the web server’s local settings.<br />

The strftime() function is very useful for displaying dates <strong>and</strong> times in a variety of different<br />

ways to make your pages more user friendly.<br />

Converting Between <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong> Date<br />

Formats<br />

Dates <strong>and</strong> times in <strong>MySQL</strong> are h<strong>and</strong>led in ISO 8601 format.Times work relatively intuitively,<br />

but ISO 8601 requires you to enter dates with the year first. For example, you<br />

could enter March 29, 2008, either as 2008-03-29 or as 08-03-29. Dates retrieved from<br />

<strong>MySQL</strong> are also in this format by default.<br />

Depending on your intended audience, you might not find this function very user<br />

friendly.To communicate between <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong>, then, you usually need to perform<br />

some date conversion.This operation can be performed at either end.<br />

When putting dates into <strong>MySQL</strong> from <strong>PHP</strong>, you can easily put them into the correct<br />

format by using the date() function, as shown previously. One minor caution if you are<br />

creating them from your own code is that you should store the day <strong>and</strong> month with<br />

leading zeros to avoid confusing <strong>MySQL</strong>.You can use a two-digit year, but using a fourdigit<br />

year is usually a good idea. If you want to convert dates or times in <strong>MySQL</strong>, two<br />

useful functions are DATE_FORMAT() <strong>and</strong> UNIX_TIMESTAMP().<br />

The DATE_FORMAT() function works similarly to the <strong>PHP</strong> function but uses different<br />

formatting codes.The most common thing you want to do is format a date in normal<br />

American format (MM-DD-YYYY) rather than in the ISO format (YYYY-MM-DD)<br />

native to <strong>MySQL</strong>.You can do this by writing your query as follows:<br />

SELECT DATE_FORMAT(date_column, ‘%m %d %Y’)<br />

FROM tablename;<br />

The format code %m represents the month as a two-digit number; %d, the day as a twodigit<br />

number; <strong>and</strong> %Y, the year as a four-digit number. A summary of the more useful<br />

<strong>MySQL</strong> format codes for this purpose is shown in Table 21.4.<br />

Table 21.4<br />

Code<br />

Format Codes for <strong>MySQL</strong>’s DATE_FORMAT() Function<br />

Description<br />

%M Month, full text<br />

%W Weekday name, full text<br />

%D Day of month, numeric, with text suffix (for example, 1st)<br />

%Y Year, numeric, four digits<br />

%y Year, numeric, two digits<br />

%a Weekday name, three characters<br />

%d Day of month, numeric, leading zeros

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!