php Laravel 5.3 日期验证器:等于或在 start_date 之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41342818/
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-08-26 02:23:08  来源:igfitidea点击:

Laravel 5.3 date validator: equal to or after start_date

phplaravelvalidationlaravel-5.3

提问by Paul Z.

I'm using Laravel 5.3 to validate start_date and end_date for an event. end_date should be equal to start_date or the after date. end_date >= start_date

我正在使用 Laravel 5.3 来验证事件的 start_date 和 end_date。end_date 应等于 start_date 或之后的日期。end_date >= start_date

$validator = Validator::make($data, [
    'start_date'    => 'required|date',
    'end_date'      => 'required|date|after:start_date',
]);

I tried to use after, but it only works for end_date > start_date. Of course, I can add custom rule using Validator::extend, but I'd like to know if we can do without adding custom rule.

我尝试使用after,但它仅适用于 end_date > start_date。当然,我可以使用 添加自定义规则Validator::extend,但我想知道我们是否可以不添加自定义规则。

Is there any way to add negative rule or add >= rule?

有没有办法添加否定规则或添加 >= 规则?

Thanks

谢谢

回答by Sujiraj R

$validator = Validator::make($data, [
    'start_date'    => 'required|date',
    'end_date'      => 'required|date|after_or_equal:start_date',
]);

Use after_or_equal

使用after_or_equal

回答by Foued MOUSSI

upgarate to 5.4and you can use after_or_equal see https://laravel.com/docs/5.4/validation#rule-after-or-equal

升级到5.4,您可以使用 after_or_equal 参见 https://laravel.com/docs/5.4/validation#rule-after-or-equal

回答by lowerends

Actually, you can also use after_or_equaland before_or_equalwhen using at least Laravel version 5.3.31. This may help to avoid having to upgrade to a higher Laravel version.

实际上,您也可以使用after_or_equalbefore_or_equal至少在使用 Laravel 版本时5.3.31。这可能有助于避免升级到更高的 Laravel 版本。

回答by James Bond

Be careful when you set a validation rule after_or_equal:nowand date_formatwith a format without hours, minutes or seconds!

当您设置验证规则after_or_equal:now并且date_format格式没有小时、分钟或秒时要小心!

For example:

例如:

$validationRules = [
    'my_time_start' => [
        'date_format:Y-m-d',// format without hours, minutes and seconds.
        'after_or_equal:now'
    ]
];

Laravel passing all date fields into the strtotime()function. Including nowstring. And strtotime('now')will return a unix timestamp with current minutes, hours and seconds.

Laravel 将所有日期字段传递给strtotime()函数。包括now字符串。而strtotime('now')会返回一个Unix时间戳与当前的分钟,小时,秒

For example, the today date is 2020-05-03. When you send a date value 2020-05-03into your script, Laravel will pass 2 values into the strtotime()for compare:

例如,今天的日期是2020-05-03。当您将日期值发送2020-05-03到脚本中时,Laravel 会将 2 个值传递strtotime()给比较:

strtotime('2020-05-03'); // always 1588489200
strtotime('now'); // NOT PREVIOUS VALUE, a different value each second, timestamp including current minutes, hour and seconds.

And you will always fail a validation (exclude a 1 second of the day).

并且您将始终无法通过验证(一天中的 1 秒除外)。

To fix it, you should use:

要修复它,您应该使用:

$validationRules = [
    'my_time_start' => [
        'date_format:Y-m-d',// format without hours, minutes and seconds.
        'after_or_equal:' . date('Y-m-d'), // not 'now' string
    ]
];