Monday, November 16, 2020

PHP | checkdate() Function

PHP checkdate() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP checkdate() Function.

The built-in checkdate() function accepts the month, day, year of a date as parameters and, verifies whether it is Gregorian date or not.

Syntax

Following below is the syntax to use this function -

checkdate ( int $month , int $day , int $year )


Parameter Details

Sr.NoParameter & Description
1

month

This is an integer value representing the month of a date, it must be between between 1 and 12.

2

day

This is an integer value representing the day of a date, it must be below the allowed number of days in the given month.

3

year

This is an integer value representing the year of a date, it must be between between 1 and 32767.


Return Value

This built-in PHP function returns a boolean value. This value is true if the given date is valid and, false if it is invalid.

PHP Version

This function was first introduced as part of core PHP version 4, and works with all the later versions.

Example1

Following example below shows the usage of checkDate() function -

<?php
   var_dump(checkdate(11, 07, 1989));
   var_dump(checkdate(02, 31, 2008));
   
   $bool = (checkdate(06, 03, 1889));
   print($bool);
   print("\n");
   print("result: ".checkdate(13, 30, 2005));
?>

Output

When the above code is executed, it will produce the following result -

bool(true)
bool(false)
1
result:

Example2

In this example, we are trying to verify the dates of leap year(s) -

<?php
   var_dump(checkdate(02, 30, 2004));
   var_dump(checkdate(02, 28, 2008));   
   var_dump(checkdate(05, 31, 2020));
   var_dump(checkdate(06, 31, 2020));
?>

Output

When the above code is executed, it will produce the following result -

bool(false)
bool(true)
bool(true)
bool(false)

Example 3

Following example below verifies whether the date 12/12/2005 is Gregorian, or not -

<?php
   $bool = checkdate(12, 12, 2005);
   
   if($bool){
      print("Given date is valid");
   }else{
      print("Given date is invalid");
   }
?>

Output

When the above code is executed, it will produce the following result -

Given date is valid


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_create() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.