You can use Strtotime () to convert a date ($date) to a timestamp, and then use Date () in the format you want to validate to a date to verify that the date is the correct format for the $date comparison.
Regular validation Date format
$dateTime = "2010-6-4 00:00:00″;
if (Preg_match ("/^d{4}-d{2}-d{2} d{2}:d {2}:d {2}$/s", $dateTime))
{
echo "Yes";
}else{
echo "No";
}
Example
/*
* Method IsDate
* function to determine whether the date format is correct
* Parameter $str Date string
$format Date format
* Return None
*/
function Is_date ($str, $format = ' y-m-d ') {
$unixTime _1=strtotime ($STR);
if (!is_numeric ($unixTime _1)) return false; If it is not a number format, it returns directly
$checkDate =date ($format, $unixTime _1);
$unixTime _2=strtotime ($checkDate);
if ($unixTime _1== $unixTime _2) {
return true;
}else{
return false;
}
}
Note that the above judgment method is sufficient for general requirements, but not very strict, for 2012-03-00 or 2012-02-31 This format date will return true, I have not found a better solution
The following code verifies that the date is in the form of the 2015-08-11 20:06:08:
<?php
Header ("Content-type:text/html;charset=utf-8");
$date = ' 2015-08-11 20:06:08 ';
if (date (' y-m-d h:i:s ', Strtotime ($date)) = = $date)
{
echo ' yes ';
}
Else
{
Echo ' no ';
}
?>
So to verify that the date format is 2015-08-11, you can change to if (date (' y-m-d ', Strtotime ($date)) = = $date) to determine, verify other formats, and so on.