Validating Dates

You want to validate the dates.

Use checkdate() to check whether the arguments it receives are valid date values. It returns TRUE if the date is valid, and FALSE if not. Valid date values are:

checkdate (month, day, year)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Validating Dates</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<h2>Validating Dates</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Month:
	<input type="text" name="mm" size="50" value="07" />
	<br />
	Day:
	<input type="text" name="dd" size="50" value="04" />
	<br />
	Year:
	<input type="text" name="yy" size="50" value="2008" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$mm = "$_POST[mm]";
	$dd = "$_POST[dd]";
	$yy = "$_POST[yy]";
	if (checkdate($mm, $dd, $yy)) {
		echo "the date is valid<br />";
	}
	else {
		echo "the date is invalid<br />";
	}
	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n";
}


?>
</p>
</body>
</html>

View the effect


Date and Time | Introduction | Date and Time in PHP | strftime() | time() | Unix Timestamps | getdate() | Validating Dates | Calculating Dates | Date and Time Functions | Formatting the Date and Time
© 2008: Hann So
email: hso@voyager.deanza.edu