dongyanfeng0546 2012-06-13 15:54
浏览 23
已采纳

PHP checkdate变种

I found a php function checkdate() , but strangely enough it only seems to accept data in format of int $month , int $day , int $year. However I am passing the date as a string (example "2012-06-13") so I came up with this workaround, because I would only allow date entered in such format. Unfortunately I am feeling this is both insecure and not a nice approach to the problem:

function CheckAdditional($value)
{
    $data = explode("-", $value);

    return checkdate($data[1], $data[2], $data[0]);
}

Question: is there a better way to check whether the date is valid?

  • 写回答

6条回答 默认 最新

  • doupuchao4256 2012-06-18 07:16
    关注
    <?php
    
    function CheckAdditional($value)
    {
        return date('Y-m-d', strtotime($value)) == $value;
    }
    
    ?>
    

    After several tests from both me and the people who tried to help me with my answer, I came up with this which suits me perfectly fine and is both easy and really reliable solution in my opinion as none was able to prove it wrong so far.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?