Check on date format..

Discussion in 'PHP' started by adamjblakey, Aug 29, 2007.

  1. #1
    Hi,

    I have a form with a field called available date which i need to make sure is correct. When posted i want to check that the date they entered is the current year e.g. 2007 and also that the entry is in this format yyyy-mm-dd.

    Any ideas anyone?

    Cheers,
    Adam
     
    adamjblakey, Aug 29, 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('-', $_POST['date']));
    
    if ($year != date('Y') OR !checkdate($month, $day, $year))
    {
        // Wrong year or non-existing calendar date.
    }
    
    PHP:
     
    nico_swd, Aug 29, 2007 IP
  3. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #3
    or use a regular expression to validate syntax:
    ([0-9]{4}-[0-9]{2}-[0-9]{2})
    Code (markup):
     
    m0nkeymafia, Aug 29, 2007 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    cheers nico, just what i am after :)
     
    adamjblakey, Aug 29, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I think i spoke to soon. I am entering the following date 2007-08-13 and it is not allowing this to go through even though it is valid.

    Any ideas?
     
    adamjblakey, Aug 29, 2007 IP
  6. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Dude I never knew about that checkdate function, thats the tits!
    Cheers
     
    m0nkeymafia, Aug 29, 2007 IP