我找到了一个 php 函数 checkdate() , 但奇怪的是它似乎只接受格式为 int $month , int $day , int $year
的数据。但是我将日期作为字符串传递(示例 "2012-06-13"
)所以我想到了这个解决方法,因为我只允许以这种格式输入日期。不幸的是,我觉得这既不安全又不是解决问题的好方法:
function CheckAdditional($value)
{
$data = explode("-", $value);
return checkdate($data[1], $data[2], $data[0]);
}
问题:有没有更好的方法来检查日期是否有效?
最佳答案
你可以试试:
function checkDateFormat($date){
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;}
致谢:http://roshanbh.com.np/2008/05/date-format-validation-php.html
关于PHP 检查日期变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11018768/