SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: Date validation

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    England
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date validation

    Hi.

    Ive used the code
    PHP Code:
    <?php
    $day 
    $_GET['day'];
    $month $_GET['month'];
    $year $_GET['year'];

    $date date("m-d-Y"mktime(000$day$month$year));
    ?>
    to collect the date that the user has typed in, but how can i make sure the user types in a correct date?

    So the cant have a month as like number "22", or a day as "56th"?

    Thanks, Cobra

  2. #2
    SitePoint Enthusiast Redprince's Avatar
    Join Date
    May 2004
    Location
    Salzgitter, Lower Saxony, Germany
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you simple have to check this
    Code:
    if($day > 0 && $day < 32 && $month > 0 && $month < 13)
    {
      date()
    }
    May the use of is_integer() is needed
    I am not part of the allesburner. I am the allesburner.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Or give them three drop down select menus.
    That way there is no confusion.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Enthusiast Redprince's Avatar
    Join Date
    May 2004
    Location
    Salzgitter, Lower Saxony, Germany
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But there are still some evil men outside trying to manipulate any data found. Better check the data given, even it is from a checkbox or so.
    I am not part of the allesburner. I am the allesburner.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I agree!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could combine the eregi function (to make sure the input is only a number, and 1-2 digits long) then use if statements to make sure it's in range, but like they said above, just use drop down menus.

  7. #7
    SitePoint Enthusiast Redprince's Avatar
    Join Date
    May 2004
    Location
    Salzgitter, Lower Saxony, Germany
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would not use any function that requires the regex-engine for that task. A simple strlen() and is_integer() should be much faster.
    I am not part of the allesburner. I am the allesburner.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    England
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers guys will get onto it now

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    England
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.

    I like spikeZ idea about the drop down menu's, but how could i limit them to only inserting correct dates?

    Some months have less days than others.. How could i make sure that the drop down list doesnt show day 31 for Feb, etc?

    Thanks for your help, Cobra

  10. #10
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cobra123
    Hmm.

    I like spikeZ idea about the drop down menu's, but how could i limit them to only inserting correct dates?

    Some months have less days than others.. How could i make sure that the drop down list doesnt show day 31 for Feb, etc?

    Thanks for your help, Cobra
    find smarter clients.

    j/k actually you could go through alot of work and make it so the first selected value (the month) will populate the next drop down menu (through javascript) with the correct days. That data could be pulled out of a text file i suppose.

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    set up an array with the acual number of days the month should have and compare that with the incoming days/month combo.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Gibb
    find smarter clients.
    LMAO, you know it's true!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    England
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol @ the clients joke
    ts not the clients i need to be worried about really, its the sorting that will be done to the dates to display them in the correct order.

    Also, i have no idea how to make it so the month box affect the days box.

    Cobra

  14. #14
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cobra123
    lol @ the clients joke
    ts not the clients i need to be worried about really, its the sorting that will be done to the dates to display them in the correct order.

    Also, i have no idea how to make it so the month box affect the days box.

    Cobra
    I dont either cause Javascript scares me. Your best bet might be to verify the date after it is submitted (like spikeZ stated). I just went to a site with date input ( www.hertz.com ) and if you put in Feb 31 and Nov 31 (both which are not valid) it'll say they're invalid on the next page.

  15. #15
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't the checkdate() function do that?

  16. #16
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jhorra
    Doesn't the checkdate() function do that?

    by jove you're correct. That's quite a handy function

    Quote Originally Posted by PHP manual
    <?php
    var_dump(checkdate(12, 31, 2000));
    var_dump(checkdate(2, 29, 2001));
    ?>

    The above example will output:

    bool(true)
    bool(false)

  17. #17
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    England
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so how would i use that?

    I dont think ive seen the output bool() before :S
    or the var_dump for that matter :P

  18. #18
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cobra123
    so how would i use that?

    I dont think ive seen the output bool() before :S
    or the var_dump for that matter :P
    me neither, but i'm assuming var_dump displays the type of variable returned and it's value.

    So i would assume if i typed
    var_dump("the quick brown fox");
    it would output
    string(the quick brown fox)

    You dont have to use var_dump you just need something like this:
    PHP Code:

    if (checkdate($month$day$year)) {
        echo 
    'valid date';
    } else {
        echo 
    'invalid date';


  19. #19
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mktime() will not return an incorrect timestamp as long as it is within the range of 1970-2038 or so.


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
  •