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.

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

Validating Dates with checkdate()<br />

You can use the checkdate() function to check whether a date is valid.This capability<br />

is especially useful for checking user input dates.The checkdate() function has the following<br />

prototype:<br />

int checkdate (int month, int day, int year)<br />

It checks whether the year is a valid integer between 0 <strong>and</strong> 32,767, whether the month<br />

is an integer between 1 <strong>and</strong> 12, <strong>and</strong> whether the day given exists in that particular<br />

month.The function also takes leap years into consideration when working out whether<br />

a day is valid.<br />

For example,<br />

checkdate(2, 29, 2008)<br />

returns true, whereas<br />

checkdate(2, 29, 2007)<br />

does not.<br />

Formatting Timestamps<br />

You can format a timestamp according to the system’s locale (the web server’s local settings)<br />

using the strftime() function.This function has the following prototype:<br />

string strftime ( string $format [, int $timestamp] )<br />

The $format parameter is the formatting code that defines how the timestamp will be<br />

displayed.The $timestamp parameter is the timestamp that you pass to the function.This<br />

parameter is optional. If no timestamp is passed as a parameter, the local system timestamp<br />

(at the time the script is run) is used. For instance, the following code<br />

<br />

displays the current system timestamp in four different formats.This code will produce<br />

output similar to the following:<br />

Friday<br />

03/16/07<br />

03/16/07 21:17:24<br />

2007

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

Saved successfully!

Ooh no, something went wrong!