2010-07-15 35 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; 
    } 
} 
?> 

感謝