Laravel 表单验证规则`Date`无法验证相对时间格式?
规则为 date
,传递相对时间 "now"
或者 "+1 year"
都无法通过验证
这些相对时间应该都是在php合法时间格式中所支持的
date 规则源码:
/**
* Validate that an attribute is a valid date.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateDate($attribute, $value)
{
if ($value instanceof DateTimeInterface) {
return true;
}
if ((! is_string($value) && ! is_numeric($value)) || strtotime($value) === false) {
return false;
}
$date = date_parse($value);
return checkdate($date['month'], $date['day'], $date['year']);
}
值通过strtotime
函数没问题,在进行date_parse
解析时发现解析结果全是零值,打印"now"
的解析结果如下:
故无法通过最后的
checkdate
函数,查了下date_parse
函数,发现此函数接受和strtotime
一样的格式,那么为什么会无法解析呢?
推荐文章: