[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 940
  • Last Modified:

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.
0
juliancaldwellomaha
Asked:
juliancaldwellomaha
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now