php 如何在laravel中验证时间

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

How to validate time in laravel

phplaravellaravel-validation

提问by Vikash

I want to validate time in Laravel. Ex:- I want that when user input the time between 8 PM to 10 PM then it will show the validation error. How can I achieve that in Laravel

我想在 Laravel 中验证时间。例如:- 我希望当用户输入晚上 8 点到晚上 10 点之间的时间时,它会显示验证错误。我怎样才能在 Laravel 中实现这一点

回答by Skysplit

Probably this code would work in your controller. However it won't validate times from different days (eg 9pm - 3am next day). time_startand time_endin this case should be provided as HH:mmbut you can change it easily.

这段代码可能适用于您的控制器。但是,它不会验证不同日期的时间(例如晚上 9 点 - 第二天凌晨 3 点)。time_starttime_end在这种情况下,应提供作为HH:mm,但你可以很容易地改变它。

public function store(Illuminate\Http\Request $request)
{
    $this->validate($request, [
        'time_start' => 'date_format:H:i',
        'time_end' => 'date_format:H:i|after:time_start',
    ]);

    // do other stuff
}

回答by Chathuranga Tennakoon

Use date_format rule validation

使用 date_format 规则验证

date_format:H:i

From docs

来自文档

date_format:format

The field under validation must match the format defined according to the date_parse_from_format PHP function.

验证中的字段必须匹配根据 date_parse_from_format PHP 函数定义的格式。

回答by Anil Stha

Create DateRequest and then add

创建 DateRequest 然后添加

<?php

namespace App\Http\Requests\Date;

use App\Http\Requests\FormRequest;


class DateRequest extends FormRequest
{
    /**
     * --------------------------------------------------
     * Determine if the user is authorized to make this request.
     * --------------------------------------------------
     * @return bool
     * --------------------------------------------------
     */
    public function authorize(): bool
    {
        return true;
    }


    /**
     * --------------------------------------------------
     * Get the validation rules that apply to the request.
     * --------------------------------------------------
     * @return array
     * --------------------------------------------------
     */
    public function rules(): array
    {
        return [

            'start_date' => 'nullable|date|date_format:H:i A',
            'end_date' => 'nullable|date|after_or_equal:start_date|date_format:H:i A'
        ];
    }
}

回答by Parithiban

Try This code

试试这个代码

use Validator;
use Carbon\Carbon;


$timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM  it works
$time = Carbon::parse($timeHours)->format('H');


$request['time'] = $time;
$validator = Validator::make($request->all(), [
    'time' => ['required','integer','between:20,22']
]);


 if ($validator->fails()) {
    dd($validator->errors());
}

enter image description here

在此处输入图片说明

回答by Umbert P.

You probably should take a look to thisbit of the documentations. A custom rule sounds like your way to go.

您可能应该看看部分文档。自定义规则听起来像是您要走的路。

回答by Alexandre Ribeiro

In Lavavel 5.6:

在 Lavavel 5.6 中:

Inside the file located in /app/Http/Requests/YourRequestValidation

在位于 /app/Http/Requests/YourRequestValidation 的文件中

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YourRequestValidation extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'initial_time' => 'required',
            'end_time' => 'required',
            'end_time' => 'after:initial_time'
        ];
    }

    /**
     * Custom message for validation
     *
     * @return array
     */
    public function messages()
    {

        return [
            'initial_time.required' => 'Please, fill in the initial time',
            'end_time.required' => 'Please, fill in the end time.',
            'end_time.after' => 'The end time must be greater than initial time.'
        ];

    }

}

回答by Adrián Prieto

$beginHour = Carbon::parse($request['hour_begin']);
        $endHour = Carbon::parse($request['hour_end']);
        if($beginHour->addMinute()->gt($endHour)){
            return response()->json([
                'message' => 'end hour should be after than begin hour',
            ], 400);
        }