Codeignitor Date formatting Check (prior to submission)

I need to allow the user to submit a date and time mm/dd/yyyy hh:mm on a form.  I would like to not use javascript to validate the entry is valid, prior to submitting to the database.

I know about the date validation in CI; however, I do not see anything for date and time together.  Does anyone have a good example of using a standard input box, taking the submission to the controller, validating that the data is a valid date and time and then submitting to the database?  

Any help would be greatly appreciated.  I have a function that converts the date to the approriate strings for mktime in PHP, but maybe I need to check via a regular expression?  Thoughts?  Suggestions?  Examples?  All greatly appreciated.
juliancaldwellomahaAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
Hello juliancaldwellomaha,

Try this : checkDT("12/25/2009 25:01")


<?php
    function checkDT($dt) {
        // Check the datetime format and put split it in $parts
        if(preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2})$/", $dt, $parts)) {
            // Check the date with checkdate and the time
            if(checkdate($parts[1],$parts[2],$parts[3]) && (intval($parts[4])>=0) && (intval($parts[4])<24) && (intval($parts[5])>=0) && (intval($parts[5])<60)) 
                return true;
            else
                return false;
        }
        else
            return false;
    }
    
    if(checkDT("12/25/2009 25:01")) {
        // Good Date and Time
    }
    else {
        // Bad Date and Time
    }
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.