Need to check if date is in correct mysql datetime format

Discussion in 'PHP' started by meroving, Jun 30, 2010.

  1. #1
    I need a function that will take a string as a parameter and return a boolean if the format is correct for entering into a mysql database as a datetime field like so: "2010-02-01 00:00:00"

    I can't seem to find a way to do this on the net... anyone got a clue?
     
    Last edited: Jun 30, 2010
    meroving, Jun 30, 2010 IP
  2. meroving

    meroving Peon

    Messages:
    135
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I found this but it only validates dates, not datetimes. Anyone good with regular expressions that can modify this for me?

    
    
    <?php
    /**
     * Checks date if matches given format and validity of the date.
     * Examples:
     * <code>
     * is_date('22.22.2222', 'mm.dd.yyyy'); // returns false
     * is_date('11/30/2008', 'mm/dd/yyyy'); // returns true
     * is_date('30-01-2008', 'dd-mm-yyyy'); // returns true
     * is_date('2008 01 30', 'yyyy mm dd'); // returns true
     * </code>
     * @param string $value the variable being evaluated.
     * @param string $format Format of the date. Any combination of <i>mm<i>, <i>dd<i>, <i>yyyy<i>
     * with single character separator between.
     */
    function is_valid_date($value, $format = 'dd.mm.yyyy'){
        if(strlen($value) >= 6 && strlen($format) == 10){
           
            // find separator. Remove all other characters from $format
            $separator_only = str_replace(array('m','d','y'),'', $format);
            $separator = $separator_only[0]; // separator is first character
           
            if($separator && strlen($separator_only) == 2){
                // make regex
                $regexp = str_replace('mm', '(0?[1-9]|1[0-2])', $format);
                $regexp = str_replace('dd', '(0?[1-9]|[1-2][0-9]|3[0-1])', $regexp);
                $regexp = str_replace('yyyy', '(19|20)?[0-9][0-9]', $regexp);
                $regexp = str_replace($separator, "\\" . $separator, $regexp);
                if($regexp != $value && preg_match('/'.$regexp.'\z/', $value)){
    
                    // check date
                    $arr=explode($separator,$value);
                    $day=$arr[0];
                    $month=$arr[1];
                    $year=$arr[2];
                    if(@checkdate($month, $day, $year))
                        return true;
                }
            }
        }
        return false;
    }
    ?>
    
    
    
    Code (markup):
     
    meroving, Jun 30, 2010 IP
  3. meroving

    meroving Peon

    Messages:
    135
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    easy peazee, I need to learn regex.
    
    
    $value = trim [php.net]($value); 
    if (preg_match("/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/", $value)) { 
     print "it's a match"; 
    }
    
    
    Code (markup):
     
    meroving, Jun 30, 2010 IP
  4. norbertrenc

    norbertrenc Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    For anyone who found this page via Google: this preg_match expression isn't 100% correct.
    If the hour is, for example, "09", it won't match.
    The text I made bold in the above expression needs to be changed to: [0-9]
     
    norbertrenc, Jan 13, 2012 IP