#Carbon date: filter article within a date range

14 messages · Page 1 of 1 (latest)

opal solstice
#

I have this intervals date send by the frontend

expires_interval : "2023-05-11T10:40:45.915Z,2023-05-18T10:40:45.915Z".

On the backend i have the following code that filters stock_entry_lines within a date range forexample between [2023-05-11, 2023-05-18]

$dates = explode(',',$request->input('expires_interval'));
            $query = $query->whereBetween('stock_entry_lines.expires_at', [
                Carbon::parse($dates[0])->toDateString(),
                Carbon::parse($dates[1])->toDateString()
            ]);

in the database i have this on the expires_at column

2023-05-18 09:31:00

the issue is that i'm not getting back the stock_entry_lines within this range while in my database i have a stock_entry_lines with this date 2023-05-18 09:31:00 where am i wrong please

molten tartan
#

I’m guessing when you’re calling toDateString on 2023-05-18T10:40:45.915Z, it’s just truncating it to 2023-05-18. If you want to include results for that day as well, then use dates and times; not just dates.

#
// Split the interval at the comma
[$start, $end] = explode(',', $request->input('expires_interval'));

$query = $query->whereBetween('stock_entry_lines.expires_at', [
    Carbon::parse($start),
    Carbon::parse($end),
]);

I also hope you’re actually validating that expires_interval request value, because just one wrong character and your code is going to error.

opal solstice
#

let me show you my vuejs code

molten tartan
#

Why?

#

Your controller or whatever in Laravel should still be validating anything sent from the front-end.

opal solstice
#

ok i see

opal solstice
#

is it possible to get this date format ```js
2023-05-18 09:31:00

opal solstice
#

i see how to get this format of date

molten tartan
opal solstice
#

Yeah. Thanks I solve the issue

molten tartan
opal solstice
#

ok i will read how to do it.

molten tartan