How to Check Valid Date Format in PHP?

Problem:

Perhaps user entered date in the form and now, you want to check if it is in right date format before further processing the data or inserting it into the database.

Solution:

There are few ways to verify a valid date format. We’ll investigate two of those below-

Method 1: Using date_parse() function

If you pass a valid date format to the function date_parse(), it will return an associative array containing detail about the date like day, month, year etc. Alone with those info, the function also returns warning and error numbers as warning_count and error_count keys respectively in the array if the supplied date is not a valid date format .

<?php
$dtInfo = date_parse("32th June 2014");
if($dtInfo['warning_count'] == 0 && $dtInfo['error_count'] == 0 ){
echo "It is a valid date.";
}else{
echo "It is not a valid date.";
}
?>

[wpdm_file id=145]

Output:
It is not a valid date.

Please note that date_parse() function accepts those date formats that are accepted by strtotime()  function. See the list of supported date and time formats. So, if you use these formats, the date_parse() function will return true.

Method 2: Using DateTime class

PHP provides an excellent wealth of ready made classes to help you developers solve common problems. To solve the date & time issues, PHP provides DateTime class.

There are 2 steps to check a valid date format using DateTime class-

  • Create a new DateTIme object using createFromFormat() method which allows you to create a DateTIme object according to the format you specified in its parameters.
  • Then, use getLastErrors() method to check if there is any error or warning in the object you just created.

In the following example, we’ll check whether 2014-04-31 is a valid date. Note: April has 30 days. So, it is not a valid date.

<?php
$date = DateTime::createFromFormat('Y-m-d', '2014-04-31');
$err = $date->getLastErrors();
if($err['warning_count'] == 0 && $err['error_count'] == 0)
echo "It's a valid date";
else
echo "It's not a valid date";
?>

[wpdm_file id=146]

Output:
It’s not a valid date

Line 2 Here, we create a DateTime object($date) using createFromFormat() method with our supplied date(‘2014-04-31’). The second parameter is our supplied date and the first one is the representation of character formatting of our supplied date. To see the list of other formatting character, check formatting character list.
Line 3 If the DateTime object($date) we created in the previous line has any error or warning, we can get to know it by using getLastErrors() method. This method returns an array with all errors and warnings. We name that array as $err.
Line 4 The number of errors and warnings are stored in the error_count and warning_count keys respectively in the array. Here, we check if any error or warning exists. If it has no warning or errors, then we print “It’s a valid date” otherwise, “It’s not a valid date”. As the date has warning, it will display the next message.

You may have different date format. In that case, match the formatting characters in the first arguments in createFromFormat() method with your supplied date in the second parameter. Ex.

<?php
$date = DateTime::createFromFormat('jS M Y', '11th Jun 2014');
…
?>