Jump to content

how to validate a date is formatted correctly


jasonc

Recommended Posts

<?php
$date = "2010-04-27"; // yyyy-mm-dd
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $regs)) {
    echo "$date is a valid format.<br />";

if(checkdate($regs[2], $regs[3], $regs[1])) {
	echo "$date is a valid date.<br />";
	// the $date is both a value format and a valid date
} else {
	echo "$date is not a valid date.<br />";
}
} else {
    echo "$date is not a valid format.<br />";
}	
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.