Issues Questions PHP Date Format Validation for pickadate.js field

asked on

Hi Saran,

Thank you for your help on my previous question. Your code is working great. Could I impose on you for one last thing?

I have added a field that uses the Amsul pickadate.js addon. Could you help me with the php validation on the date that is input into this field?

I have researched php date validation techniques, but am unsure which is best and how to implement using your code.

According to the date picker the date submitted format is yyyy/mm/dd.

Regards,

Dave

P.S. I am not sure if you need to see my code. let me know.

    Add Answer
    All HTML codes will be stripped in the comment, except inline <code> HTML tag.
    1 Answer
    1 answered on

    If you are using PHP 5.3.0+ you can use DateTime::createFromFormat to validate a date format, it returns a new date or false on failure, hope it helps.

    function validate_date($date){
        $mdate = DateTime::createFromFormat('Y/m/d', $date);
        if($mdate){
            return true;
        }
    }
    
    //usage
    if(validate_date("2017/03/03")){
        print 'valid format';
    }
      All HTML codes will be stripped in the comment, except inline <code> HTML tag.

      Can You Answer this? Help us answer the question!

        • Answer
        Supports basic Markdown syntax. Excluded syntaxes (Headers, Lists, Blockquotes, Table, Images)