-1

I want a regular expression to use in preg_match to check whether or not a date is in the pattern DD/MM/YYYY. I tried the code below, but it didn't work:

preg_match("#^[1-31]*1[1-12]*1[1950-2013]?#", $date);
Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
  • 4
    pretty much everything about it is wrong. – Spudley Sep 03 '12 at 21:58
  • 3
    This is really poor question, think about others taking their time to answer your question. Please be more patient before asking questions. There's no effort visible in this question. – Martin. Sep 03 '12 at 21:59
  • And it's not exactly hard to google either. Possible duplicate of [What is the MM/DD/YYYY regular expression and how do I use it in php?](http://stackoverflow.com/questions/2520633/what-is-the-mm-dd-yyyy-regular-expression-and-how-do-i-use-it-in-php) – mario Sep 03 '12 at 22:12
  • but now i have Charles Hooper's answer it is : date_parse_from_format('d/M/Y', $date); >>> it is good answer it's helped me – Emadeldeen Abd-Allah Abdel-Hal Sep 03 '12 at 23:41

3 Answers3

4

Your regex translates to:

  • From the start of the string,
  • Look for any number of characters in the range 1 through 3, or the character 1
  • Look for a literal 1
  • Look for any number of characters in the range 1 through 1, or the character 2
  • Look for a literal 1
  • Look for either one or zero of the characters 1, 9, 5, 0 through 2, 0, 1 or 3

It very obviously doesn't make sense.

You can't validate a date in regex, since dates are not regular. You have different numbers of days in each month, leap years, and a bunch of other stuff. You would be better off separating your date into pieces (use explode) and check it with checkdate()

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • "You can't validate a date in regex" - actually we can generate all the possible valid dates in a particular range and combine them with `|` – zerkms Sep 03 '12 at 22:04
  • I won't be surprised that someone used that sometime. People wonder me every day with terrible solutions they choose to solve their problems ;-) – zerkms Sep 03 '12 at 22:06
1

Your regex is so wrong that I don't even know where to start explaining.

But the simplest answer is: don't use regex for this. PHP has perfectly good date handling routines already. For example, DateTime::createFromFormat()...

$date = DateTime::createFromFormat('Y/m/d', $dateString);
echo $date->format('j-M-Y');  //outputs something like '15-Feb-2009'

See http://www.php.net/manual/en/datetime.createfromformat.php

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

You could use preg_match("/^\d{1,2}\/\d{1,2}\/\d{4}/",$date); however a regex isn't well-suited for this task because there you will still need to perform additional validation ("9999" is a valid year, for example). I think instead you may want to take a look at date_parse_from_format(), like:

date_parse_from_format('d/M/Y', $date);

See Convert String To date in PHP for more information.

Community
  • 1
  • 1
Charles Hooper
  • 2,739
  • 1
  • 19
  • 16