簡體   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