Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to validate the date using preg_match function in PHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 223
    Comment on it

    The syntax of the preg_match function is that:

    preg_match (string pattern, string string [, array pattern_array],[, int $flags [, int $offset]]]);

    In the above function first and second parameter are mandatory and the rest of the parameters are optional. So, we only use the mandatory parameter to validate the date. For example we have a given date and use preg_match function to validate it:

    $date="2015-07-21";
    if (preg_match('/^((19|20)\\d\\d)[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/',$date))
    {
            return true;
        }else{
            return false;
        }

    In above scenario preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: