-6

In PHP, I have a string that I need to check if it is the following format: XXXX-YY-ZZ

Where x,y and z are integers. So the string contains 3 integers separated by 2 dashes with the following rules:

xxxx > 2003 
yy >= 0 and yy <= 12 
zz >= 1 zz <= 31

What would be the easiest way to validate this?

Eric Leschinski
  • 135,913
  • 89
  • 401
  • 325
Andrei
  • 1

1 Answers1

1
function validateDate($date)
{
    $d = DateTime::createFromFormat('Y-m-d', $date);
    return $d && $d->format('Y-m-d') == $date;
}

function was copied from this answer or php.net

Glavić
  • 41,315
  • 13
  • 72
  • 105
Davide Pastore
  • 8,512
  • 10
  • 39
  • 51