Problem with date format

0

You see, I have the following form, which is to extract the rows of a table that are between a certain date:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Mostrar las ofertas creadas entre 2 fechas</div>

                <div class="card-body">
                    <form method="POST" action="fechas_oferta" novalidate>
                        @csrf

                        <div class="form-group row">
                            <label for="fecha_inicial" class="col-md-4 col-form-label text-md-right">Fecha inicial</label>

                            <div class="col-md-6">
                                <input id="fecha_inicial" type="date" class="form-control{{ $errors->has('fecha_inicial') ? ' is-invalid' : '' }}" name="fecha_inicial" value="{{ old('fecha_inicial') }}" required autofocus>

                                @if ($errors->has('fecha_inicial'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('fecha_inicial') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="fecha_final" class="col-md-4 col-form-label text-md-right">Fecha final</label>

                            <div class="col-md-6">
                                <input id="fecha_final" type="date" class="form-control{{ $errors->has('fecha_final') ? ' is-invalid' : '' }}" name="fecha_final" value="{{ old('fecha_final') }}" required autofocus>

                                @if ($errors->has('fecha_final'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('fecha_final') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="sector" class="col-md-4 col-form-label text-md-right">Sector</label>

                            <div class="col-md-6">
                                <select id="sector" class="form-control{{ $errors->has('sector') ? ' is-invalid' : '' }}" name="sector" value="{{ old('sector') }}" required autofocus>
                                    @foreach($sectores as $sector)
                                        <option value="{{$sector->nombre}}">{{$sector->nombre}}</option>
                                    @endforeach
                                </select>

                                @if ($errors->has('sector'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('sector') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Generar PDF
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

And here is where I register the rows that are created between these dates:

$ofertas=Oferta::where(date('d-m-Y', strtotime('created_at')),'>=',date('d-m-Y', strtotime($request->fecha_inicial)))->where(date('d-m-Y', strtotime('created_at')),'<=',date('d-m-Y', strtotime($request->fecha_final)))->where('sector',$request->sector)->orderBy('created_at','desc')->get();

However, I get this error:

I need to pass the format from update_at to simply "year-month-day", since it will make it easier for me to make the comparisons. How do I achieve it?

Edit: I had an idea. This is the Offer table:

Schema::create('ofertas', function (Blueprint $table){
        $table->increments('id');
        $table->string('titulo');
        $table->text('descripcion');
        $table->string('empresa');
        $table->string('sector');
        $table->date('fecha_limite');
        $table->timestamps();
    });

Thanks to the "timestamp" I get the variable created_at (which has the format año-mes-dia hora-minuto-segundo ) with which I compare dates. My idea is to remove this variable and replace it with a date that I will call 'creation_date' (which will only be in año-mes-dia format). The story is how I have done since then to insert or modify the table without an error message because the predefined attributes of the creation and modification dates are missing. If this is not possible, the alternative is to change the format of the created_at variable, so that it shows only the year, month and day, but not the hour, minute or second.

    
asked by Miguel Alparez 11.06.2018 в 17:58
source

1 answer

0

I have already found a solution. Pay attention to this code:

$ofertas=Oferta::whereDate('created_at','>=',$request->fecha_inicial)->whereDate('created_at','<=',$request->fecha_final)->where('sector',$request->sector)->orderBy('created_at','desc')->get();

When using whereDate instead of a where to dry, the variables are automatically formatted to "year-month-day", allowing the comparison to be made.

    
answered by 11.06.2018 в 20:22