Hi @stallyons
do you think, it is possible to do diff your times correctly, if you don't have dates with your times? How can Carbon or any other library know, that 03:00:00 is after midnight from your data?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How can i get difference between two times without date. I.e departure time = 07:00:00 arrival time = 03:00:00 (Midnight) expected difference = 20 hours
Note : the format is in 24 hours.
when i'm trying to get difference I'm getting wrong difference
$departure = $schedule->terminals->where('terminal_id',$request['from'])->first()->departure_time;
$arrival = $schedule->terminals->where('terminal_id',$request['to'])->first()->arrival_time;
dd(Carbon::parse($departure)->diffInHours(Carbon::parse($arrival)));
// 4 hours which wrong
Issue Solved. If difference is negative add day to date.
$diff = Carbon::parse($departure)->diffInHours(Carbon::parse($arrival),false);
$arrival_date = $diff < 0 ? $date->toImmutable()->addDay()->format('Y-m-d') : $date->format('Y-m-d');
Please or to participate in this conversation.