Home PHP How to validate a date format using PHP?
 
 
 

How to validate a date format using PHP?

PDF Print E-mail

Question: How to validate a date format using PHP? To check whether the date entered is in the correct format

NOTE: We will use the following date format within this example YYYY-MM-DD

 

Answer:

  • Use the preg_match 'Perform a regular expression match'  PHP function available for PHP 4 & 5 
  • Find the correct expression
  • And use the following code to check for the date format
    if(preg_match('/[12]\d{3}-[01]\d-[0123]\d/',$date)){
        $msg = "Valid date";
    }else{
        $msg = "Invalid date";
    }
+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!