user455318
user455318

Reputation: 3346

validate date - problem php

i am trying this code to validate a date but i get false. what is the reason of that?

function validate_age($form) {
        $str = "1977/03/27";

        $stamp = strtotime( $str );

        if (!is_numeric($stamp)) {
            echo ("nop");
            return FALSE;
        }

        $year  = date( 'Y', $stamp );
        $month = date( 'm', $stamp );
        $day   = date( 'd', $stamp );


        if (checkdate($year, $month, $day)){
            return TRUE;
        }

        return FALSE; //stops here
}

validate_age($form);

thanks

Upvotes: 0

Views: 160

Answers (2)

Romeo M.
Romeo M.

Reputation: 3278

month supposed to be from 1 to 12 (no zeros in front)

checkdate($month, $day, $year); as in checkdate(3, 27, 1977); not checkdate(03, 27, 1977); so for month you should use $month = date( 'n', $stamp );

Upvotes: 0

dynamic
dynamic

Reputation: 48091

Check the order of your arguments

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

It's all wrote in the php official docu: http://it.php.net/checkdate

Upvotes: 3

Related Questions