InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Validating a Date in PHP
PHP

Validating a Date in PHP

InfinityCoder November 18, 2016

You want to check if a date is valid. For example, you want to make sure a user hasn’t provided a birthdate such as February 30, 1962.

Use checkdate():

1
2
3
4
// $ok is true - March 10, 1993 is a valid date
$ok = checkdate(3, 10, 1993);
// $not_ok is false - February 30, 1962 is not a valid date
$not_ok = checkdate(2, 30, 1962);

The function checkdate() returns true if $month is between 1 and 12, $year is between 1 and 32767, and $day is between 1 and the correct maximum number of days for $month and $year.

Leap years are correctly handled by checkdate(), and dates are rendered using the Gregorian calendar.

Because checkdate() has such a broad range of valid years, you should do additiona validation on user input if, for example, you’re expecting a valid birthdate.

The longest confirmed human life span is 122 years old.

To check that a birthdate indicates that a user is between 18 and 122 years old, use the checkbirthdate() function shown in Example 3-18.

Example 3-18. checkbirthdate()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function checkbirthdate($month,$day,$year) {
   $min_age = 18;
   $max_age = 122;
   if (! checkdate($month,$day,$year)) {
   return false;
   }
 
   $now = new DateTime();
   $then_formatted = sprintf("%d-%d-%d", $year, $month, $day);
   $then = DateTime::createFromFormat("Y-n-j|",$then_formatted);
   $age = $now->diff($then);
   if (($age->y < $min_age)|| ($age->y > $max_age)) {
       return FALSE;
   }
   else {
       return TRUE;
   }
}
// check December 3, 1974
if (checkbirthdate(12,3,1974)) {
    print "You may use this web site.";
} else {
    print "You are too young (or too old!!) to proceed.";
}

The function first uses checkdate() to make sure that $month, $day, and $year represent a valid date. If they do, it builds two DateTime objects: one for “right now” and one representing the passed-in month, day, and year.

The call to sprintf() normalizes the passed-in values as integers with no leading zeros, which matches what’s expected by the Y-n-j format string given to DateTime::createFromFormat().

The trailing | in the format string tells DateTime::createFromFormat() to initialize the unspecified hour, minute, and second time parts to zero.

Once the two DateTime objects are built, determining whether the specified birthdate produces an age within the acceptable range is just a matter of calling Date Time::diff() and then checking the resultant DateInterval object to see if its y property, containing the number of years in the date interval, is appropriate.

The function returns true if the supplied date is exactly $min_age years before the current date, but false if the supplied date is exactly $max_age years after the current date.

That is, it would let you through on your 18th birthday, but not on your 123rd.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Adding to or Subtracting from a Date in PHP
You need to add or subtract an interval from a …

Adding to or Subtracting from a Date in PHP

Reading the POST Request Body in PHP
You want direct access to the body of a request, …

Reading the POST Request Body in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap