title: Validate date format in php
body: <p>I'm trying to validate date using PHP.</p>
  
  <p>I'd like following formats to be valid:</p>
  
  <pre><code>d/m/yy
  d/m/yyyy
  dd/m/yy
  dd/m/yyyy
  d/mm/yy
  d/mm/yyyy
  dd/mm/yy
  dd/mm/yyyy
  </code></pre>
  
  <p>I've tried many regular expressions and different variations of checkdate() function. Currently I have something like this:</p>
  
  <pre><code>function _date_is_valid($str)
  {
       if(strlen($str) == 0)
           return TRUE;
       if(substr_count($str,'/') == 2)
       {
           if (preg_match("/^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2}))/", $str))
           {
               $datearray = explode('/',$str);
               if($datearray[2] &gt; 2030)
                   return FALSE;
               return checkdate($datearray[1], $datearray[0], $datearray[2]);
           } 
           else 
           {
               return FALSE;
           }
       }
       return FALSE;
  }
  </code></pre>
  
  <p>This however, validates dates like 11/11/200 and 11/11/200#</p>
  
  <p>How can I validate date to match required format?</p>
  
  <p>Edit: I could check datearray[2] to be between 10 and 30 and 2010 and 2030. But is there a way to check it using regex?</p>
  
  <p>Edit1: return TRUE on strlen($str) == 0 is because I want users to be able to add events without knowing when will the event occur so someone else can qualify the schedule and assign event to certain date later</p>
  
  <hr>
  
  <p>Just for the record. I ended up doing:</p>
  
  <pre><code>function _date_is_valid($str)
  {
      if(strlen($str) == 0) //To accept entries without a date
          return TRUE;
      if(substr_count($str,'/') == 2)
      {
          list($d,$m,$y) = explode('/',$str);
          if(($y &gt;= 10 &amp;&amp; $y &lt;= 30) || ($y &gt;= 2010 &amp;&amp; $y &lt;= 2030))
          {
              return checkdate($m,$d,$y);
          }
      }
      return FALSE;
  }
  </code></pre>
  
  <p>Thanks for all your answers</p>