DotDragnet
February 08, 2012, 10:31:45 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Help decide how we fund DDN and where we take the community from here. Post in the thread all about it: http://www.dotdragnet.com/forum/index.php/topic,4368.0.html
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: preg_match for dates  (Read 632 times)
neal
Sr. Member
****
Posts: 300



nealio82
View Profile Awards
« on: February 11, 2010, 02:15:31 PM »

ahhhh, regex; the one thing we all try our hardest to avoid despite their overwhelming power.

i got hold of a regex online from http://www.felgall.com/php17.htm and i want to match dates in the yyyy-mm-dd format.

the code below is just returning false every time, can anyone see why..?

Code:
$regexPattern = "^(19|20)\d{2}[\/\.- ](0?[1-9]|1[0-2])[\/\.- ](0?[1-9]|[12][0-9]|3[01])$";

    if(trim($postArray[$this->data]) != "") {
        if(!preg_match("/" . $regexPattern . "/", trim($postArray[$this->data]))) {
            return FALSE;
        } else {
            if($this->dataType == "time") {
                return (string) $this->data . ":00";
            } else {
            return (string) $this->data;
            }
        }
    } else {
        return FALSE;
    }

hmmm, just turned on error reporting & got
Quote
Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 19


in the meantime, i shall google further...
Logged

sig edited due to migrating from .net
neal
Sr. Member
****
Posts: 300



nealio82
View Profile Awards
« Reply #1 on: February 11, 2010, 02:30:57 PM »

haha!

found the answer on a german site, i escaped the two - signs.

although it also accepts yyyy-mm-d as an acceptable format, how do i get it to always require double digit values for m & d?
Logged

sig edited due to migrating from .net
spannaa
Full Member
***
Posts: 203



Spannaa
View Profile WWW Awards
« Reply #2 on: February 11, 2010, 03:06:09 PM »

This:

Code:
(19|20)\d{2}-([0][0-9]|[1][0-2])-([0][0-9]|[1][0-9]|[2][0-9]|[3][0-1])

ought to do what you're after but this:

Code:
(19|20)\d{2}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))

is better because it won't accept too may days in a month e.g. 2009-11-31 is rejected.
Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #3 on: February 11, 2010, 03:23:42 PM »

But that still allows yyyy-02-29

Regex is not the way to do this. See checkdate().
Logged
neal
Sr. Member
****
Posts: 300



nealio82
View Profile Awards
« Reply #4 on: February 11, 2010, 03:57:39 PM »

brilliant, cheers guys.

i've gone with the regex spannaa posted for now simply because this is the 3rd level of error interception on the date inputs. there's jquery date picker as the preferred option, then there's the handy "dates must be entered to this format" text in the label if the user has JS disabled, and finally the regex. there shouldn't be an issue with requiring the fallback as it's a small custom app being used by 15 people in my office.

i'll probably use checkdate at some point in the near future, but i haven't got time to modify the class at the moment.

thanks again smile
Logged

sig edited due to migrating from .net
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!