Laravel - 使用规则集的日期进行日期验证后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24062639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:36:45  来源:igfitidea点击:

Laravel - after date validation with date from rules set

validationlaravellaravel-4

提问by haakym

I'm trying to validate two dates to ensure one is greater than the other using the aftervalidate rule. I have my rules set like this:

我正在尝试使用after验证规则验证两个日期以确保一个日期大于另一个日期。我的规则是这样设置的:

$rules = array(
 'date_from' => 'required|date_format:"d/m/Y"', 
 'date_to'   => 'required|date_format:"d/m/Y"|after:date_from',
 // more rules ...
);

When using the following values: date_from= "01/06/2014" and date_to= "01/06/2014" OR date_to= "12/06/2014" everything is hunky dory, however.. using anything above 12 for day fails i.e. date_to= "13/06/2014" to date_to= "31/06/2014"

使用以下值时: date_from= "01/06/2014" 和date_to= "01/06/2014" OR date_to= "12/06/2014" 一切都很好,但是.. 使用 12 天以上的任何值都失败,即date_to= " date_to13/06/2014"到= "31/06/2014"

I've also tried this and it gives the same results:

我也试过这个,它给出了相同的结果:

$dateFromForm = Input::get('date_from');

$rules = array(
 'date_from'  => 'required|date_format:"d/m/Y"', 
 'date_to'    => 'required|date_format:"d/m/Y"|after:' . $dateFromForm,
);

Quite clearly to me it's reading the day as the month, any ideas what I'm doing wrong here?

对我来说很清楚,它正在将一天读为一个月,任何想法我在这里做错了什么?

Thanks

谢谢

采纳答案by Laurence

If you look at the Laravel docs - it says

如果您查看 Laravel 文档 - 它说

The dates will be passed into the PHP strtotime function.

日期将被传递到 PHP strtotime 函数中。

The problem with strtotimeis that it assumes you are using m/d/Y. If you want d/m/Y- you need to change it to d-m-Yto be correctly parsed.

问题strtotime在于它假设您正在使用m/d/Y. 如果需要d/m/Y- 您需要将其更改d-m-Y为正确解析。

So change your date format to d-m-Yand it will work.

因此,将您的日期格式更改为d-m-Y它会起作用。