PHP Dates and Times Validating a Date - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Validating a Date - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, May 12, 2019

PHP Dates and Times Validating a Date

PHP Dates and Times





Validating a Date


Problem

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.

Solution

           Use checkdate():
           // $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);


Discussion

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 additional 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.

Example   checkbirthdate()

            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::createFrom Format(). The trailing | in theformat 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.


No comments:

Post a Comment

Post Top Ad