SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP regex needed for dd/mm/yyyy format

    Hi all,

    As per the title, I need a regular expression in the dd/mm/yyyy to work with the pregmatch function. I have tried online but not having much joy, I keep getting "unknown modifier errors".

    Any help MUCH appreciate.

    Kind regards and thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What are you planning on doing with it? Are you using it for validation or are you needing to capture the individual elements?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    What are you planning on doing with it? Are you using it for validation or are you needing to capture the individual elements?
    Hello Anthony, Simply to validate incase the user has javascript turned off.

    Kind regards

  4. #4
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match("^\d{1,2}/\d{2}/\d{4}^""datehere"); 
    Made the first part 1 or 2 in case they put 7 for July instead of 07.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you're just needing a rather crude implementation with no chekcing of the actual values, you could use...
    PHP Code:
    function isDate($date){
      return 
    === preg_match(
        
    '~^[0-9]{1,2)/[0-9]{1,2)/[0-9]{4)~',
        
    $date
      
    );

    If you're wanting, what seems to be, the mac daddy of patterns...
    PHP Code:
    #dd/MM/yyyy with leap years 100% integrated Valid years : from 1600 to 9999
    function isDate($date){
      return 
    === preg_match(
        
    '~^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$~',
        
    $date
      
    );

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    If you're just needing a rather crude implementation with no chekcing of the actual values, you could use...
    PHP Code:
    function isDate($date){
      return 
    === preg_match(
        
    '~^[0-9]{1,2)/[0-9]{1,2)/[0-9]{4)~',
        
    $date
      
    );

    If you're wanting, what seems to be, the mac daddy of patterns...
    PHP Code:
    #dd/MM/yyyy with leap years 100% integrated Valid years : from 1600 to 9999
    function isDate($date){
      return 
    === preg_match(
        
    '~^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$~',
        
    $date
      
    );


    AnthonySterling - Helping people prevent those 17th century goons from registering.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Zurev View Post
    AnthonySterling - Helping people prevent those 17th century goons from registering.
    Ha ha. I spy a new signature!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anythony, im sure those regex's work great but its not working for me. I am finishing of a clients existing validation script, so I did not write it from scratch as I would have prefered.

    My client is using this script for validation :-

    http://www.benjaminkeen.com/software/php_validation/


    And the validation function is being called thus: -


    PHP Code:
    $dateRegExp "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$";
        
    $rules[] = "required,StartDate,Please enter a valid Start date";
        
    $rules[] = "reg_exp,StartDate, ".$dateRegExp.",i, Please enter your Start Date in the format dd/mm/yyyy"
    $errors validateFields($_POST$rules); 
    Ive been able to use all kinds of regular expresions with it, but for some reason its not likeing dates....
    Last edited by johnuk; Jan 9, 2011 at 10:22. Reason: extra info

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Could the space after StartDate, be an issue? Try removing it. Your other attempts would probably work then too.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Could the space after StartDate, be an issue? Try removing it. Your other attempts would probably work then too.
    Oh yeah looks like it Just one thing, it accepts a single 0 as a date?

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It doesn't this end.

    PHP Code:
    var_dump(
      
    isDate(0)
    ); 
    # bool(false)

    var_dump(
      
    isDate('0')
    ); 
    # bool(false) 
    Maybe the library you're using is adding another layer of complexity there?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does on the form however may I pm you the link to the form possibly to see what I mean?

  13. #13
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    It doesn't this end.

    PHP Code:
    var_dump(
      
    isDate(0)
    ); 
    # bool(false)

    var_dump(
      
    isDate('0')
    ); 
    # bool(false) 
    Maybe the library you're using is adding another layer of complexity there?
    Highly likely! I prefer to write my own, but I have no choice on this occasion.

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The problem lies in the library you're using.
    Code:
    if (!empty($fields[$field_name]) && !preg_match($reg_exp, $fields[$field_name]))
    PHP's empty function considers '0' empty.
    The following things are considered to be empty:

    • "" (an empty string)
    • 0 (0 as an integer)
    • "0" (0 as a string)
    • NULL
    • FALSE
    • array() (an empty array)
    • var $var; (a variable declared, but without a value in a class)

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  16. #16
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh wonderful ! What is the best way to remedy this?

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Personally, for speed, I'd add a minimum length requirement and be done with it for now.

    Say, 8?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  18. #18
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Personally, for speed, I'd add a minimum length requirement and be done with it for now.

    Say, 8?
    Good idea!!

  19. #19
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't want to hijack, but I'm curious if anyone has any thoughts on how to lock down the simpler one by allowing -./

    Except! They have to match, ya know?
    PHP Code:
    ~^\d{1,2}[/.-]\d{2}[/.-]\d{4}$~ 
    That works, except it will allow things like 10/7.1984
    So possibilities:
    10.7.1984
    10-7-1984
    10/7/1984

    Uniformity! Any ideas?

  20. #20
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off the top of my head, I'd normalise the string to something expected then test that. It would keep the RegExp as focused as possible, or rather, simple as possible.

    Maybe...
    PHP Code:
    function isDate($date){
      
    $date str_replace(array('.''-''\\'), '/'$date);
      return 
    === preg_match(
        
    '~^[0-9]{1,2)/[0-9]{1,2)/[0-9]{4)~',
        
    $date
      
    );

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #21
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zurev View Post
    Uniformity! Any ideas?
    You could capture the separator in a group and then use a back reference to match what was captured.

    For example, ([ab])c\1 will match aca and bca, but not acb or bca.

    Quote Originally Posted by AnthonySterling View Post
    Maybe...
    Do you want to allow any trailing characters after the date? (i.e. there's nothing anchoring the pattern at the end)
    Salathe
    Software Developer and PHP Manual Author.

  22. #22
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    You could capture the separator in a group and then use a back reference to match what was captured.

    For example, ([ab])c\1 will match aca and bca, but not acb or bca.



    Do you want to allow any trailing characters after the date? (i.e. there's nothing anchoring the pattern at the end)
    If you're asking me, then no.

    So perhaps a back reference to capture whether they used ./- for the first separator, then require it for the second?

  23. #23
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Was painful! But I got it nonetheless, for anyone whose interested:
    PHP Code:
    /**
     * Matches dates with period, hyphen, or slash as the seperater
     * however they have to be the same.
     * I.E. 10.5-1975 won't return true, however 10.5.1975 OR 10-5-1975 will
     **/
    function isDate($date)
    {
        return (
    preg_match("~^\d{1,2}([/.-])\d{2}\\1\d{4}$~"$date)==1) ? TRUE FALSE;

    Not extremely useful, just more of a backreferencing test for me? :-p

  24. #24
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Do you want to allow any trailing characters after the date? (i.e. there's nothing anchoring the pattern at the end)
    No, sorry. 't was a typo.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •