繁体   English   中英

在php中以DD-MM-YYYY”格式验证日期

[英]Validate date in DD-MM-YYYY” Format in php

可以使用php验证此格式10-7-2010吗? 使用此脚本:

$array = explode('-', $str);
$day = $array[0];
$month = $array[1];
$year = $array[2];
$Valid = checkdate($month, $day, $year);

我有这个错误:

checkdate() expects parameter 2 to be long

如何验证这种格式? 提前致谢

尝试更改为: $Valid = checkdate((int)$month, (int)$day, (int)$year);

编辑:我尝试了您的代码,它工作正常,可能是您尝试传递某处的日期格式不正确吗?

edit2:2007年10月7日是有效日期,但听起来您认为不应该这样吗? 它是过去的日期,因此,如果您要确定某个日期是否是将来的日期,则可以执行以下操作:

$isValid = ( strtotime("$year-$month-$day") > time() );

并感谢您的帮助。 我终于得到了完整的代码(在其他条件下)。

我将其发布在这里,希望对您有所帮助。

<?php
function valid_date($date) // if the date is valid, then the function returns true. else returns false.
{
    $array = explode('/', $date, 3);
    if (strlen($date) == 10 && substr_count($date, '/') == 2 && checkdate($array[1], $array[0], $array[2]) && substr_count($date, ' ') == 0) {
        return true;
    } Else {
        return false;
    }
}
?>

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM