the 2 ways to validate date in php you should know now

0
119
the 2 ways to validate date in php you should know now
the 2 ways to validate date in php you should know now

to validate date is very necessary in data collection. We illustrate how you can simply validate date using php simple function known as chechdate().

Reasons to validate date

  • to ensure the given day in specified month of the year provided in the date is exist
  • to be able to sanitize the date because future transactional uses such birthday wish, expiration date and age etc.

Objectives for writing on how to validate date using php checkdate()

Well in this post we considered doing the date validation using php. though there are more lot of other programming languages like jquery, python which can do same.

Don’t worry we’re going to do it with php for now then stay tuned we will also use other programming languages to validate date as well, but for now let’s focus on using php to do the job. Back to our topic

There are atleast two method proven to be acceptable used to validate date in php

  1. using checkdate
  2. using range and in_array
    check these articles validate date input without checkdate in php (alternative) and Proven ways to use checkdate: (validate date in php for payment card and birthday)

Well in the above two method the generally used acceptable method is checkdate function

how the php checkdate() works

php checkdate function simply handles date validation uniquely that it ensures that every date parameters in a given date are properly defined else it will throw false

Checkdate function does 3 things at same time

  1. check if month is integer and valid (with in the range of 1-12 month)
  2. check if the given year is integer as well
  3. checks if the given day exist within the range of number of days within the given month
    for example if the given day is 31st of February is will return false because february never had 31st day

understanding the php checkdate()

let’s tour a little, headover to https://www.php.net/manual/en/function.checkdate.php for more clarification. Have you seen the descriptions and example given in the checkdate php official site.

This checkdate function is bool function which means it returns output as yes / no (true/false). so in our example we are going to construction a logic that does the validation. You can get a date to use from our formless bootstrap input group with no form control.

The date format format to be used

get a date in the format that is generally used while writing date in php, we are using this date 2012-04-18

see our example below

 <?php 
$ourdate = '2012-04-18';
?>

convert the date to array

Let’s try to convert the date to array to enable us split it into units according to date parameters respectively.

 <?php 
$ourdate = explode('-', $ourdate);
$theyear = $ourdate[0]; //the year provided in the date
$themonth = $ourdate[1]; //the month provided in the date
$theday = $ourdate[2]; //the day provided in the date
?>

performing the date validation with checkdate()

Now let’s perform the date validation with the generally used method in php which is the checkdate() function, see below.

 <?php 
if(checkdate($themonth, $theday, $theyear) == true){
	echo 'the date provided is valid';
}else{
	echo 'the date provided is invalid';
}
?>

Finally, This is nice if you run test now you will get “the date provided is valid”.

Auch!… sorry if you get the other while using same date as ours then you made a mistake. correct the mistake and continue with us. Now try to modify the given date to 2011-02-30 it should return “the date provided is invalid”,You understand the whole scenarion it should return false because the month of february does not have 30 days it only end in 28 or 29 days.

LEAVE A REPLY

Please enter your comment!
Please enter your name here