2010-07-15 15 views
0

这种格式10-7-2010可以用php进行验证吗? 有了这个脚本:在DD-MM-YYYY中验证日期“格式在php

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

我有这样的错误:

checkdate() expects parameter 2 to be long 

我如何可以验证这样的格式? 在此先感谢

+2

在这里工作! http://codepad.viper-7.com/0idtfL – Artefacto 2010-07-15 15:39:10

回答

3

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

编辑:我想你的代码,它工作正常,也可能是你有一个格式化的日期不好的地方你想在通过?

edit2:10-7-2007是一个有效的日期,但它听起来像你认为它不应该是?这是过去的日期,所以如果你正试图确定一个日期是在未来,你可以做这样的事情:

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

+0

他没有理由不得不这样做。这只会隐藏错误。 – Artefacto 2010-07-15 15:40:03

+0

谢谢,现在没有任何错误。但是,10-7-2007是有效的:( – TheNone 2010-07-15 15:40:46

+0

是的,因为10-7-2007是有效的日期... – Rob 2010-07-15 15:42:32

0

,并感谢您的帮助球员。我终于得到了一个完整的代码(和其他条件)。

我在这里发布它,希望它可以帮助某人。

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

感谢