验证错误日期为 PHP


Validation Error dates PHP

我正在尝试在 PHP 中验证日期,但问题是对于某些日期工作,一个例子是:"02/2/2015"返回 true,"20/12/2015"错误返回,这是一个严重的问题,我在代码中没有看到错误。

功能。

<?php
function check_date($date) {
    $open_date  = explode('/', $date);
    if (count($open_date) == 3) {
        if (checkdate($open_date[0], $open_date[1], $open_date[2])) {
            return true;
        } else {
         return false;
        }
    } else {
        return false;
    }
}
//$date = "02/2/2015"; // return true !
$date = "20/12/2015"; // return false ?
if(check_date($date)) {
    echo "valid";
} else {
    echo "invalid";
}
?>

如何解决这个问题?

checkdate期望按以下顺序排列一个月、一天和一年:

https://secure.php.net/manual/en/function.checkdate.php

如果您的日期格式为日/月/年,那么您仍然可以使用 checkdate ,您只需更改参数的顺序:

if (checkdate($open_date[1], $open_date[0], $open_date[2]))

checkdate 函数的签名看起来像checkdate(month,day,year);。您最多可以有 12 个月而不是 20 个月。:-)