Hi, I need to make a query between two date and time.
$data = Modelo::whereBetween('created_at', ['2018/11/10 12:00', '2018/11/11 10:30'])
->get();
But doing this query brings me all the results between the dates without taking into account the hours.
Dates are automatically saved with Eloquent
And apart I do the following to format the dates that I am going to look for:
Carbon::now()->parse($date)->format('Y-m-d H:i:s');
To be able to do a search between 2 dates, within
Laravel
you must proceed as follows:If the field of your database is of type,
DATETIME
the structure of your query must be as follows:Or also
You must keep in mind that for the calculation to be done as you require; your field should have this structure:
YYYY-MM-DD HH:MM:SS
->2018-11-12 12:52:12
If for example you are going to occupy Carbon
It can be like this:
What about the FormattedDate property?
If you do this:
var_dump($fechaFormateada);
You will get the following result
That as you can see if it includes the seconds; to respect the data format required by the field
DATETIME
What you can do is place two inputs like this in your html
the datetime-local attribute will take both the date and the time since it is what you need and in your query you can use it in this way
eye in your controller you must pass request in this way
I hope I have helped you and solved your question