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.

21<br />

Managing the Date <strong>and</strong> Time<br />

IN THIS CHAPTER, WE DISCUSS CHECKING AND FORMATTING the date <strong>and</strong> time <strong>and</strong> converting<br />

between date formats.These capabilities are especially important when you are<br />

converting between <strong>MySQL</strong> <strong>and</strong> <strong>PHP</strong> date formats, Unix <strong>and</strong> <strong>PHP</strong> date formats, <strong>and</strong><br />

dates entered by the user in an HTML form.<br />

Key topics covered in this chapter include<br />

n Getting the date <strong>and</strong> time in <strong>PHP</strong><br />

n Converting between <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong> date formats<br />

n Calculating dates<br />

n Using the calendar functions<br />

Getting the Date <strong>and</strong> Time from <strong>PHP</strong><br />

Way back in Chapter 1,“<strong>PHP</strong> Crash Course,” we described using the date() function<br />

to get <strong>and</strong> format the date <strong>and</strong> time from <strong>PHP</strong>. Here, we discuss this function <strong>and</strong> some<br />

of <strong>PHP</strong>’s other date <strong>and</strong> time functions in a little more detail.<br />

Using the date() Function<br />

As you might recall, the date() function takes two parameters, one of them optional.<br />

The first one is a format string, <strong>and</strong> the second, optional one is a Unix timestamp. If you<br />

don’t specify a timestamp, date() will default to the current date <strong>and</strong> time. It returns a<br />

formatted string representing the appropriate date.<br />

A typical call to the date() function could be<br />

echo date(‘jS F Y’);<br />

This call produces a date in the format 19th June 2008.The format codes accepted by<br />

date() are listed in Table 21.1.

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

Saved successfully!

Ooh no, something went wrong!