Search code examples
laravellaravel-5laravel-validation

validate unix timstamp in controller by laravel validation


is there anyway to validate incoming unixtime's parameter to server and compare that second one is greather that first one by laravel validation? my code only validate date format

$this->validate($request, [
            'start_date'    => 'required|date',
            'end_date'   => 'required|date|after_or_equal:start_date',

        ]

Solution

  • A timestamp is a number and can be anything between 0 and 2147483647. That's all you can validate. (And of course that one is bigger than the other.)

    So, you could do the following:

    $this->validate($request, [
        'start_date' => 'required|numeric',
        'end_date' => 'required|numeric|gte:start_date',
    ]);
    
    • numeric will make sure the input is a number
    • gte checks if the value is greater than or equal to another number.