dongzhuohan7085 2016-02-01 00:06
浏览 35
已采纳

验证错误日期PHP

I'm trying to validate dates in PHP, but the problem is that with some dates work, an example would be: "02/2/2015" returns true, "20/12/2015" false returns, is a serious problem and I see no error in the code.

Function.

<?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";
}

?>

How could solve this problem?

  • 写回答

2条回答 默认 最新

  • douruhu4282 2016-02-01 00:16
    关注

    checkdate expects a month, day and year, in that order:

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

    If your dates are formatted as day/month/year then you can still use checkdate, you'll just have to change the order of the parameters:

    if (checkdate($open_date[1], $open_date[0], $open_date[2]))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动