Ad

Carbon Its Parsing My Date Range +1 Day

- 1 answer

I have the date range

{"from":"2018-02-12T23:00:00.703Z","to":"2018-02-13T22:59:59.703Z"}

When I do a

$periodFrom = Carbon::parse($request->from);

$periodTo = Carbon::parse($request->to);

And then I want to get the number of dates between those dates I do

 $days = $periodTo->diffInDays($periodFrom);

And for some reason its giving me 0 days instead of 1 day

Edit:

Ad

Answer

You can do this:

$days = round($periodTo->diffInHours($periodFrom) / 24);

Instead of round() you can also use ceil() method, it depends on what exactly result you want to get for 1.2 days, 1.8 days etc.

If you want to get 1 day instead of 0, but for all other cases you want to get just full days (8 for 8.9 for example), do this:

$diff = $periodTo->diffInHours($periodFrom);
$days = $diff === 0 ? 1 : $diff;
Ad
source: stackoverflow.com
Ad