Thread: Date validation

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

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

    Date validation


    Hi;

    What is the quickest method to validate this date format on php 5.3

    dd/mm/yyyy

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    182
    Rep Power
    83
    Originally Posted by English Breakfast Tea
    Hi;

    What is the quickest method to validate this date format on php 5.3

    dd/mm/yyyy

    Thanks
    Google is your friend, and so is the php manual.

    https://www.google.com.au/search?q=php+validate+date
    gave me this.

    http://us2.php.net/checkdate
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by BarryG
    Google is your friend, and so is the php manual.
    Hi

    I know Google is my friend Google also is full of countless bad php examples that make Jacques1 very angry.

    I have come up solutions that work but I am looking for the most proper.

    Thank you

    PHP Code:
    if(strlen($_POST['checking_date'])>&& (strpos($_POST['checking_date'],'/') !== false))
        {
            
    $date explode('/',$_POST['checking_date']);
            if(
    checkdate($date['1'], $date['0'], $date['2']))
                {
                    echo 
    "Valid";
                }        
        } 

    Edit
    ------
    Does this suck?
    PHP Code:
    if(strlen($_POST['checking_date'])>&& (strpos($_POST['checking_date'],'/') !== false))
        {
            
    $date explode('/',$_POST['checking_date']);
            if(!
    checkdate($date['1'], $date['0'], $date['2']))
                {
                    
    $error TRUE;
                    
    $error_message "<br />Invalid Checkin Date";    
                }    
            else     
                {    
                    
    $checkin $date['2'].'-'.$date['1']."-".$date['0'];
                }    
        }
        if(
    strlen($_POST['checkout_date'])>&& (strpos($_POST['checkout_date'],'/') !== false))
            {
                
    $date explode('/',$_POST['checkout_date']);
                if(!
    checkdate($date['1'], $date['0'], $date['2']))
                    {
                        
    $error TRUE;
                        
    $error_message "<br />Invalid Checkout Date";
                    }
                else     
                    {    
                        
    $checkout $date['2'].'-'.$date['1']."-".$date['0'];
                    }        
            } 
    Last edited by English Breakfast Tea; October 28th, 2013 at 08:03 PM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Don't repeat yourself. When you find yourself duplicating code, stop and refactor.

    In your case, you should make a separate function for checking a date string. You can also shorten the whole thing by using the built-in date parsers:

    PHP Code:
    <?php

    function validate_date($date$format 'd/m/Y') {
        
    $parsed date_parse_from_format($format$date);
        return !
    $parsed['warning_count'] && !$parsed['error_count'];
    }
    PHP Code:
    <?php

    if ( !validate_date($_POST['checking_date']) ) {
        
    $error true;
        
    $error_message "<br />Invalid Checkin Date";
    } elseif ( !
    validate_date($_POST['checkout_date']) ) {
        
    $error true;
        
    $error_message "<br />Invalid Checkout Date";
    }
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo