PHP function to check valid date

by

in

I have written a PHP function to check a supplied string is a valid date or not. Lets have a look into the code.

/**
 * The function is_date() validates the date and returns true or false
 * @param $str sting expected valid date format
 * @return bool returns true if the supplied parameter is a valid date 
 * otherwise false
 */
function is_date( $str ) {
    try {
        $dt = new DateTime( trim($str) );
    }
    catch( Exception $e ) {
        return false;
    }
    $month = $dt->format('m');
    $day = $dt->format('d');
    $year = $dt->format('Y');
    if( checkdate($month, $day, $year) ) {
        return true;
    }
    else {
        return false;
    }
}

Let’s use and improve the function.


Comments

8 responses to “PHP function to check valid date”

  1. great vhaiya

  2. Thank you bro. I have interest in php and my sql. would you help me to learn the course?

  3. This is great Solution for date validation. Was looking something simple like this. Thanks a ton

    Ravi

  4. Thanks, this works!

  5. Above Function not Worked Perfectly….
    Try urself

    is_date(“2014-02-31”);—- showing correct date while its wrong date
    is_date(“14-03-29”);————- “”

    Improve and Post Correct Function with all the functionality like to check whether year,month,date and leapyear or not ….
    Thanks
    Mahendra

  6. Peter VARGA Avatar
    Peter VARGA

    Hi,

    I am sorry but the script does not work properly. For example: 2014-02-30 is validated as a correct date.
    I wrote this function [I admit it is adapted for my purposes but can be adapted easy]:


    function isValidDate($sDateTime)
    {
    $aFormat = array("Y-m-d" , "Y-m-d H:i:s");
    $bError = FALSE;
    for ( $i = 0 ; $i < count($aFormat) ; $i++ ) {
    $d = DateTime::createFromFormat($aFormat [$i], $sDateTime);
    $aResult = DateTime::getLastErrors();
    if ( $aResult ["warning_count"] ) {
    # there is definitely something wrong
    if ( isset($aResult ["warnings"][10]) OR isset($aResult ["warnings"][19]) ) {
    # for example: 2014-02-30 or 2014-05-32 or 15:56:70
    $bError = TRUE;
    break;
    }
    }
    switch ( $aResult ["error_count"] ) {
    case 0:
    # no error - all seems OK
    break 2;
    case 1:
    # we check if "data missing"
    if ( !isset($aResult ["errors"][10]) ) {
    # no - something else
    $bError = TRUE;
    break 2;
    }
    # continue with the next format
    break;
    }
    }
    if ( $bError ) {
    return "";
    }
    return date($aFormat [$i], strtotime($sDateTime));
    }

  7. Peter VARGA Avatar
    Peter VARGA

    Unfortunately this solution does not work at all. All these dates return TRUE even it is not a valid date:
    2015-02-29
    2015-04-31
    2015-06-31 and so on.
    The problem is the DateTime constructor accepts these wrong dates and returns the next valid day.

  8. Isn’t it enough to do:

    function is_date( $str ) {
    try {
    $dt = new DateTime( trim($str) );
    return true;
    }
    catch( Exception $e ) {
    return false;
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.