Simple Date Validation..

Discussion in 'PHP' started by adamjblakey, Jul 18, 2007.

  1. #1
    hi,

    I am trying to add a date validation to my form processor so that if the date format is not in this format yyyy-mm-dd then it will echo incorrect.

    Any ideas what i would need to do anyone?

    Cheers,
    Adam
     
    adamjblakey, Jul 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    list($year, $month, $day) = array_map('intval', explode('-', $date));
    
    if (!checkdate($month, $day, $year))
    {
        // Throw error
    }
    
    
    PHP:
    Does not only check if the date is in a valid format, it does also check if the date exists.
     
    nico_swd, Jul 18, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for that nico..

    From looking at the would i have to assign the post[] value to $date for this to work?
     
    adamjblakey, Jul 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Yes.

    
    $date = $_POST['whatever-key-you-are-using'];
    
    PHP:
    Or replace $date with the $_POST variable in the code above.

    Btw, it's easier and less confusing for the user if you use drop downs with the dates. This way you'll get it in the correct format and it's just easier.
     
    nico_swd, Jul 18, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I know i wanted to do this but my customer wants pop up calenders :S

    Cheers for the quick replies :)
     
    adamjblakey, Jul 18, 2007 IP