It is very important to validate date hence you should know the easiest advance way to check if date is valid using php checkdate()
you should always use.
Now that your given date is valid (validated) we will now go ahead and accept in our application database.
so before you accept date inputs as we did always validate it.
Now let’s learn how to run the validation using php
DOWNLOAD THE RESOURCES FILES:
Before Getting Started with this guide
Before we go further just know that php have an already made function that validate checkdate,
All we are trying to do here is just a tweak to advance it and make web works easy and fast in terms of collecting data and data security.
we’re going to check if date data submitted through our form is valid before accepting it.
sometimes or mostly it is necessary to validate inputs before it is processed.
Tech programming resource used are:
programming languages php, jquery, javascript
Let’s start
wait first
3 Reasons to consider why you should never forget to validate date
- date is an input which needs an appropriate validation because sometimes invalidated date may make easy for random bot or unserious user to give false information while submitting his / her data via our form
- date validation is useful that date maybe use to output user date of birth in order to wish that user happy birthday, user’s age, users birth month or year, or check if payment card expiration date is expired or not
- unstructured / invalidated date inputs gives your troubles in future if your wish to use the date for certain needs because users supplied dates in different formats
You got that clearly
fine let’s continue with the validation
How we check if the date is valid using php : DETAILED
Head-over to php.net official site for checkdate
document https://www.php.net/manual/en/function.checkdate.php
now take a closer look at this function descriptioncheckdate(int $month, int $day, int $year): bool
Alright then, there’s more to do
You need collect data with a date input field form or use our manipulated html date input (just with bootsrap dropdowns)
the bootstrap manipulated html date input method makes the work easier than before collecting data just like traditional form method all thanks to jquery(ajax post) and bootstrap.
Both framework have done a greate job and those that tweak ajax-post to achieve it too have done so much good.
the formless date input is easy to implement try it see the demo.
Have gotten a date of your own to validate.
now let’s begin
you can see from the php checkdate function description that the prescribed date format should be in this format
month-day-year [mm-dd-yyyy]
Aw! Ours is in this format yyyy-mm-dd
Well stick tight with us in our next post you will see the right date format porpular used in php
but not worry stick in we are going to get everything right and nice.
let’s ride on
The example dates to validate
It’s time to do the business why we are here
- 1st Date: 1984-02-15
- 2nd Date: 2002-02-30
Above are date which we got from our form date input (formless bootstrap)
the first(1st) date should be correct while the second(2nd) date should be incorrect because Month(02) which is February rarely or do not have day 30 in it.

2 Step for validating the date in php
First let’s convert the date into an array in order to split the date into unit and be able to get the year, month, and day as requested by the php checkdate syntax
Good!
You should now do this
along with you We do the 1st date and test the 2nd date after ward Ok
step-1: Using php explode function (splitting given date)
We convert the date into array using the php explode function in order to split the given date into 3 parameter of date being (day, month, year) we couldn’t say respectively because certain date format is not observed.
less we forgot make sure to convert each split to an integer because the values expected by the checkdate
function are all integers.
we use php intval() function to convert the splits to integers see code below
<?php
$thedate = '1984-02-15'; //this is the date being checked
$thedate = explode('-', $thedate);
$year = intval($thedate[0]);
$month = intval($thedate[1]);
$day = intval($thedate[2]);
?>
As you can see above code we are done with converting the 1st date into array and too collected the output by splitting the date into unit according to the array index
step-2: Using PHP checkdate function (check the date split parameters)
Now the php checkdate
will come to work
it simple! isn’t it.
Alright just create logic see ours below
<?php
//since it a bool we are using logic to output it result
if(!checkdate($month, $day, $year)){
echo 'This Date is Invalid : Hey! pls. Retreat';
}else{
echo 'This Date is Valid : Continue my friend';
}
?>
Okay, the result will be valid

Let’s play around with our computer system date
Now you may change the date in the code to the 2nd date then I assure you it should be invalid because February never had 30th on calendar
But if yours is valid when you changed to the 2nd date then you are not getting correctly check your computer date and time
or your country must time travelled back date.
lol…
we’re done now
See all the code in full
More important check the right date format to use when processing date submitted via an online form