Compare dates in Laravel


I have a table called Input, which has these parameters:

public function up(){
        Schema::create('entradas', function (Blueprint $table){
            $table->unsignedInteger('persona'); // Quien ha comprado la entrada.

I want to make a form to create an entry, but with the rule that the date for which it is reserved can not be earlier than the current one.


<div align="center" class="panel panel-default">
    <h1 class="text-center text-mute"> {{ __("Nueva reserva") }} </h1>
<div class="row">
    <form method="POST" action="../reserva">
        {{ csrf_field() }}
        <div class="form-group">
            <label for="fecha" class="col-md-12 control-label"> {{ __("Indique la fecha") }}
            <input id="fecha" style="width:150px" type="date" class="form-control" name="fecha" value="{{ old('fecha') }}"/>

        <button type="submit" name="addPlanta" class="btn btn-default"> {{ __("Reservar entrada") }}
    <h1 class="text-center text-mute" style="color:#FF0000"> {{ __("Debes haber iniciado sesión para crear una reserva") }} </h1>


Route::post('reserva','[email protected]');

And here where the reservation will be validated:

public function confirmar(Request $request){
            'fecha' => 'required',


            return back()->with('message', ['msg', __('No tienes suficiente dinero')]);
                $hoy=new DateTime("now");

                    $request->merge(['persona' => auth()->id()]);
                    return back()->with('message', ['success', __('Reserva realizada con exito')]);
                    return back()->with('message', ['msg', __('No puede reservar para una fecha pasada')]);

And to my surprise, regardless of whether the date chosen is greater or less than the current one, I always get the error message because the date chosen is earlier than the current one. Now check with dd () the dates variables and although the current date gives more information (hours, minutes, etc) than the date of the form, both dates are. Should I change the format of the dates?

Edit: I've already made the comparison, but I run into this error message:

I have verified that it happens right in the Input :: create (). What will it fail now?

asked by Miguel Alparez 18.02.2018 в 12:50

1 answer


Laravel has a helper that delivers the current date and time, it's now() , plus Laravel uses Carbon, a very powerful and handy DateTime wrapper.

Perhaps the simplest thing you can do is to format the value of now () so that it delivers only the date, which should result in the same format as the field type date of html (yyyy-mm-dd) ):

if ($request->fecha >= now()->toDateString()) ...

By the way, and more as a suggestion, in the controller method you have a lot of validation logic, which would be better placed in a separate form validation file: link

Finally, and more for UX, if you know in advance that you will not allow the user to book because they have less than "7" in their balance, it is better to notify the user before they even reach the form.

answered by 18.02.2018 / 14:38