#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171

    Can't get checkdate to work!


    Hello;
    I am calling the checkdate function and send an invalid value as argument (04-30w-2013), but it returns true for some reason! I wonder why! Thanks
    PHP Code:
    /*The code below prints:
    04-30w-2013
    Good Date */
    public function valid_date($date)
        {
            
    $date_format explode('-',$date);
            echo 
    $date_format['1']."-".$date_format['2']."-".$date_format['0'];
            if(
    checkdate($date_format['1'],$date_format['2'],$date_format['0']))
                {
                    echo 
    "Good Date";
                    return 
    true;
                }
            else
                {
                    echo 
    "Bad Date";
                    return 
    false;
                }
        } 
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.

    Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Originally Posted by requinix
    checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.

    Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use.
    Thank you requinix. That's not good. Bad php. They can improve this.
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Originally Posted by requinix
    checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.

    Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use.
    How about something like this:
    PHP Code:
    public function valid_date($date)
        {
            
    $date_format explode('-',$date);
            if(!
    is_numeric($date_format['0']) || $date_format['0']<date('Y') || $date_format['0']>strtotime("+2 years"))
                {
                    
    $invalid_year=true;
                }
            if(!
    is_numeric($date_format['1']) || $date_format['1']<|| $date_format['0']>12)
                {
                    
    $invalid_month=true;
                }
            if(!
    is_numeric($date_format['2']) || $date_format['2']<|| $date_format['0']>31)
                {
                    
    $invalid_day=true;
                }
            if(!
    checkdate($date_format['1'],$date_format['2'],$date_format['0']))
                {
                    
    $invalid_date=true;
                }
             if(
    $invalid_year==true || $invalid_month==true || $invalid_day==true || $invalid_date==true)
                {
                    return 
    false;
                } 
            
        } 
  8. #5
  9. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Originally Posted by zxcvbnm
    Thank you requinix. That's not good. Bad php. They can improve this.
    Why should they change it to suit you? The function is already clearly documented to take three integer arguments. Its purpose is to validate a potential date, not your string inputs.

    Originally Posted by zxcvbnm
    How about something like this:
    A little long-winded but sure, if that works for you.

IMN logo majestic logo threadwatch logo seochat tools logo