Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Checking Dates

  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Checking Dates

    What is the best way to check a Date on a web form?

    1.) Regular Expressions?

    2.) Some PHP Date Function (that I don't know about)?

    3.) Switching from an Input Box to Drop-Down Controls so the user is forced to choose from a list of valid values??

    Although Option #3 would handle whether a date is invalid (e.g. Feb 29, 2001 or June 31, 2009)


    Debbie

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Dates can most easily be checked by using the strtotime function. If its a normal parsable date that follows the gnu datetime format rules, it will return a valid result (including 0), and on error will return false.
    The problem with the above is ambiguous types. mm/dd/yyyy is valid, as is dd.mm.yyyy, but dd/mm/yyyy is interpreted as a mm/dd/yyyy since it doesn't match the ruleset. It will still try, so if you enter 6-4-2011 or 6.4.2011, it will know you mean April 6, 2011. But if you enter 6/4/2011 it will know you mean June 4th 2011, even though the intent may have been for April 6, 2011.
    The next best option to get around the ambiguity that can come from strtotime is to use three dropdowns, one for year, one for month, and one for day, where day can go to 31. Then verify that the number of days selected matches the number of days available in a month (don't forget to calculate the leap years).

    I wouldn't use regex for this at all.

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The next best option to get around the ambiguity that can come from strtotime is to use three dropdowns, one for year, one for month, and one for day, where day can go to 31. Then verify that the number of days selected matches the number of days available in a month (don't forget to calculate the leap years).

    I wouldn't use regex for this at all.
    So how do I check for valid month/day/year combination? (That was really the initial question.)

    I have been looking at checkdate() but even if that is a good choice, what do you do when a user enters "June 31" or "Feb 29, 2001"?

    (I don't know JavaScript, so this needs to be a server-based solution...)



    Debbie

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by doubledee View Post
    So how do I check for valid month/day/year combination? (That was really the initial question.)

    I have been looking at checkdate() but even if that is a good choice, what do you do when a user enters "June 31" or "Feb 29, 2001"?

    (I don't know JavaScript, so this needs to be a server-based solution...)



    Debbie
    Your initial question was NOT the format month/day/year, although that has already been answered with an strtotime. Checkdate simply follows the rules of manual parsing. Choose the format you want to use, as integers only, and provide them to the checkdate function. You would manually need to parse the 'June 31' for example to determine that June is the 6th month. Or use strtotime which accepts textual dates.

    Actually now that I think of it, I believe strtotime provides over and underflow of days. So Feb 29, 2011 would be March 1, 2011 instead of a false. So if you want to actually validate this you'll need to parse it yourself to then check it. Which checkdate can do or a simple switch. So easiest way now is to use dropdowns for the month and day, and a dropdown or text input for the year.

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Your initial question was NOT the format month/day/year, although that has already been answered with an strtotime.
    I just meant those three parts in no particular order.


    Checkdate simply follows the rules of manual parsing. Choose the format you want to use, as integers only, and provide them to the checkdate function. You would manually need to parse the 'June 31' for example to determine that June is the 6th month. Or use strtotime which accepts textual dates.
    How accurate is strtotime?

    Is it safer to just work off Drop-Downs? (I would think so.)


    Actually now that I think of it, I believe strtotime provides over and underflow of days. So Feb 29, 2011 would be March 1, 2011 instead of a false. So if you want to actually validate this you'll need to parse it yourself to then check it. Which checkdate can do or a simple switch. So easiest way now is to use dropdowns for the month and day, and a dropdown or text input for the year.
    There are always lots of ways a free-form date could break functions like strtotime, right?

    Plus there is that whole pre-1970 epoch issue...


    Debbie

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Pre-1970 issues are mainly on windows systems, not linux, and I believe these are now long past the problem. You can provide a negative timestamp. This is only limited now by the version of PHP and the machine architecture which will collapse on January 19 2038, 03:14:08/07.
    strtotime is very accurate as long as the gnu rules are followed. Which are very abundant.


  •  

    Posting Permissions

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