Codeignitor Date formatting Check (prior to submission)

Posted on 2009-12-21
Last Modified: 2013-11-10
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.
Question by:juliancaldwellomaha
    1 Comment
    LVL 81

    Accepted Solution

    Hello juliancaldwellomaha,

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

        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;
                    return false;
                return false;
        if(checkDT("12/25/2009 25:01")) {
            // Good Date and Time
        else {
            // Bad Date and Time

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    Read about why website design really matters in today's demanding market.
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now