Date parsing in php

Discussion in 'PHP' started by Angelus, Jul 14, 2007.

  1. #1
    Hi,

    I have a problem parsing a certain form of date. I'm using preg_match to find date in a text but I just can't get it right.

    The date format is: yyyy/mm/dd

    How should I write regexp to make it work? Any help is appreciated...

    Thanks

    S.

    p.l.u.r.
     
    Angelus, Jul 14, 2007 IP
  2. Angelus

    Angelus Well-Known Member

    Messages:
    1,622
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    138
    #2
    A friend of mine found a solution :)

    Here it is:

    preg_match("/\d*\/\d*\/\d*/", $buffer, $date)

    p.l.u.r.
     
    Angelus, Jul 14, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Slightly better would be:
    
    '/\d{4}\/\d{2}\/\d{2}/'
    
    PHP:
    Otherwise it would consider the following dates as valid:
    - 1/3/
    - 12342423/12/12345124525
    - /3/
    - etc...
     
    nico_swd, Jul 14, 2007 IP
  4. Angelus

    Angelus Well-Known Member

    Messages:
    1,622
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    138
    #4
    Nice suggestion,

    thanks.

    I never looked at it in such a way because the file I'm parsing only contains yyyy/mm/dd format in it :)

    p.l.u.r.
     
    Angelus, Jul 14, 2007 IP