how to check if date is valid using php very simple method and easy way that you should know

check if date is valid - manipulate date input validation
check if date is valid - manipulate date input validation

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



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

  1. 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
  2. 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
  3. 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 official site for checkdate document
now take a closer look at this function description
checkdate(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

  1. 1st Date: 1984-02-15
  2. 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.

invalid date input - check if date is valid
invalid date input

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


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

$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

//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';
	echo 'This Date is Valid : Continue my friend';

Okay, the result will be valid

valid date input checked

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.


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


Please enter your comment!
Please enter your name here