0

When a user enters a date in text box,i've to check ,whether it is in yyyy-mm-dd format.

Note Even month,date,for eg:2012-02-32 is not valid because,date can be only till 31 and same for month,he can n't enter month as 13.

If it is in wrong format,i should echo.

Thanks in advance!

0

5 Answers 5

1

Try this

list($year,$month,$day) = explode('-', $input);
if (checkdate($month, $day, $year)) {
    // Correct
} else {
    // Incorrect
}
Sign up to request clarification or add additional context in comments.

Comments

1

Reading comments on http://php.net/manual/en/function.checkdate.php is quite informative, including validating through regexp.

I use the following code from that page:

function checkDateTime($data) {
    if (date('Y-m-d', strtotime($data)) == $data) {
        return true;
    } else {
        return false;
    }
}

Also I'd recommend adding JavaScript datepicker http://jqueryui.com/demos/datepicker/

Comments

0
$e = explode('-', '2012-02-32');
if (checkdate($e[1], $e[2], $e[0])){
    // Valid
}else{
    // Invalid
}

http://php.net/manual/en/function.checkdate.php

Comments

0

that's exactly what you need: http://php.net/manual/en/function.checkdate.php

Comments

0

You should not use regular expressions for this. A better (maybe not the best) is to use checkdate();

$parts = explode('-', $input);
if (sizeof($parts) == 3 && checkdate($parts[1], $parts[2], $parts[0])) {
    // Correct
} else {
    // Incorrect
}

1 Comment

:error on line 2Parse error: syntax error, unexpected T_BOOLEAN_AND

Your Answer

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