0

I have piece of code that works without making it a function. But, when I make it a function, it always returns false. The purpose of this function is to check if the date is valid, as the name suggests. Can anybody tell me what is wrong with the code ?

function is_valid_date($a) {
//date format Y-m-d H:i:s
if(preg_match('/^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]) ([01]\d|2[0123]):([0-5]\d):([0-5]\d)$/', $a)){ 
    list( $_date , $_time ) = explode(' ',$a);
    list ($year,$month,$day) = explode("-",$_date);
    list($hour,$minute,$second) = explode(":",$_time);

    if ($day == "31" && ($month == "4" || $month == "6" || $month == "9" || $month == "11" || $month == "04" || $month == "06" || $month == "09" ))  {
        return false;
    } elseif ($month == "2" || $month == "02") {
            if($year % 4==0){
                if($day == "30" || $day == "31"){
                    return false;
                } else {
                    return true;
                }
            }else{
                if($day == "29" || $day == "30" || $day == "31"){
                    return false;
                } else {
                    return true;
                }
            }
    }

}else{
    return false;

}
}

when I try

if (is_valid_date("2012-12-02 15:30:00")) { echo "valid date";} 

nothing is printing.

2
  • 1
    I see that your function can return true only else if month=2 Commented Dec 1, 2012 at 21:28
  • why write your own date validation routine when PHP has it all built-in already? Commented Dec 1, 2012 at 21:31

2 Answers 2

1

What's wrong with the following using checkdate?

<?php
    function is_valid_date($date) {
        if (preg_match('/^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]) ([01]\d|2[0123]):([0-5]\d):([0-5]\d)$/', $date, $matches)){
            return checkdate($matches[3], $matches[4], $matches[1]);
        }
        return false;
    }
    var_dump(is_valid_date("2012-12-02 15:30:00")); //true
?>

This will match the following:

2012-10-13 00:00:00
1990-02-30 12:34:56

but not

15:30:00
2012-12-02
2012/12/02 15:30:00
2012-13-02 15:30:00

Click here to see it running online

Sign up to request clarification or add additional context in comments.

2 Comments

i guess this works exactly as intended. but what are $matches 3 4 and 1 ?
@jinni as you can see I've added a parameter of , $matches to the preg_match call. This means that everything in parenthesises gets "matched" and outputted in this $match array which now looks like the following (using print_r, from 2012-12-02 15:30:00): Array( [0] => 2012-12-02 15:30:00 [1] => 2012 [2] => 20 [3] => 12 [4] => 02 [5] => 15 [6] => 30 [7] => 00) meaning that the 3rd is the month, the 4th is the day and the 1st parameter is the year. You can see more on the preg_match manual page: php.net/manual/en/function.preg-match.php
0

Put

return true;

before the end of the function (before the last brace } ). Otherwise in some cases, nothing is returned and it is considered false.

Whole code:

function is_valid_date($a) {
//date format Y-m-d H:i:s
if(preg_match('/^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]) ([01]\d|2[0123]):([0-5]\d):([0-5]\d)$/', $a)){ 
    list( $_date , $_time ) = explode(' ',$a);
    list ($year,$month,$day) = explode("-",$_date);
    list($hour,$minute,$second) = explode(":",$_time);

    if ($day == "31" && ($month == "4" || $month == "6" || $month == "9" || $month == "11" || $month == "04" || $month == "06" || $month == "09" ))  {
        return false;
    } elseif ($month == "2" || $month == "02") {
            if($year % 4==0){
                if($day == "30" || $day == "31"){
                    return false;
                } else {
                    return true;
                }
            }else{
                if($day == "29" || $day == "30" || $day == "31"){
                    return false;
                } else {
                    return true;
                }
            }
    }

}else{
    return false;

}
    return true;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.