SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match question

    Hi Guys,

    How can I check that a date of birth is in the correct format?

    For example it should be like this:

    Code:
    nn/nn/nn
    Thanks in advance...

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Frankfurt/Germany
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use a different approach and first check if a given date of birth is valid (we don't want February 31st, right?).

    You should give checkdate a try.

    After that, simply reformat the DoB with substr.

  3. #3
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    preg_match('~[0-9]{2}/[0-1][0-9]/[0-3][0-9]~', $date);
    assuming that you have yy/mm/dd. This will allow 07/19/39 though, but it's a start which you may tweak a bit...

    edit: with mainlinks approach being much better
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


Bookmarks

Posting Permissions

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