Results 1 to 4 of 4

Thread: Need regular expression that matches any date acceptable by strtotime()

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Need regular expression that matches any date acceptable by strtotime()

    Hey all.

    I was wondering if anyone knew of where I could find a regular expression that would match any date that is acceptable by strtotime().

    Here is what I have so far (not complete by any means):
    PHP Code:

    '#((([0-3]?[0-9](st|nd|rd|th)? 
    )?(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Ju((ly?)|
    (ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?) 
    )?(18|19|20)[0-9]{2})#i' 
    This will match any of the following:

    21 Aug 1998
    Aug 1998
    1998
    21st August 1998
    ... And any combination of variations you can think of in there.

    The problem is, strtotime() can take MANY more date formats and I want to be able to match all of them and I'm not too skilled at regular expressions. I could probably come up with one, but it would be miles long just to account for every case. I'm sure someone else could come up with a more elegant one.

    Any ideas?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Need regular expression that matches any date acceptable by strtotime()

    Hmmm. I might have found part of the answer:

    http://regexlib.com/REDetails.aspx?regexp_id=113

    That doesn't match DD mmm YYYY or DD mmmmmm YYYY, though.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Need regular expression that matches any date acceptable by strtotime()

    Why don't you use strtotime to see if it is valid?

    PHP Code:
    $date strtotime($datestring);

    if (
    $date !== false)
      
    // valid
    else
      
    // invalid 

  4. #4

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Need regular expression that matches any date acceptable by strtotime()

    Because I need to use preg_match_all() on a paragraph of text that could contain any type of date.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width