Checking a valid date entered by user in PHP

Many times we have to check the date entered are in correct format or not. The combination of entered month date and year by a user has to be a valid date to use in our applications. Even if we give a selection or a drop down list box to select a date we have to check the combination of month, day and year selection is valid or not. User may select 29th Feb 2005 (which is not a leap year ) or it may select 31st Nov of any year. So the combination has to be checked.

Date Validation using createFromFormat()

We can create date objects by using createFromFormat() by entering date and format used.
function Date_validate($input_date, $format = 'd-m-Y')
    $date_obj = DateTime::createFromFormat($format, $input_date);
    return $date_obj && $date_obj->format($format) == $input_date;

echo Date_validate('12-12-2017'); // Output is 1
echo var_dump(Date_validate('12-12-2017')); // Output is bool(true)
echo var_dump(Date_validate('31-11-2017')); // Output is bool(false)

Using checkdate()

We will try checkdate() function which takes care of leap year checking also. This function validates the date and returns true if date is correct or false if date is wrong or does not exist. Here is the format

bool checkdate (int month, int day, int year)
echo checkdate(12,31,2017); // Output 1 
echo var_dump(checkdate(12,31,2017)); // Output bool(true) 
echo var_dump(checkdate(29,02,2017)); // Output bool(false) 
If you are using the drop down date combination for selection or asking to enter date in a format, better to validate date by using checkdate function.

Here is the case where checkdate will return false

echo 'invalid date'; 
}else {
echo “Entry date is correct “;
If we are asking the user to enter date in a text field then we have to break the entered date value by using explode function and then use the checkdate function to validate the date data ( of user ). Here we are collecting the user entered date value of a form posted by POST method.

$arr=split("/",$dt); // splitting the array
$mm=$arr[0]; // first element of the array is month
$dd=$arr[1]; // second element is date
$yy=$arr[2]; // third element is year
echo "invalid date";
}else {
echo "Entry date is correct";
We can use data entered by user in a query in MySQL table for getting ( selecting ) matching records.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


More On Date Object


how to compare the data entered by the user to the data in the database?


It can be used in a sql query. A link is added now on this at the end of the tutorial above.


how to check a date already entered in database is within the range of a date and duration currently entered by a user?


In SQL section you can read this article on how to get record between two dates.


Just remember this, in SQL, valid format is 'Y-m-d' and day and month must be in 2 digits char.


send email befor one day to friends birthday


How To Find Days From Two Date ? Is there any specific function like floor or else?

Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer