Reputation: 1489
I have two fields beginning time and endingtime in a Mysql database. I want to calculate a duration inside a view in Laravel. So I wrote :
{{ $date1=date('G:i',strtotime($timeSlot->beginningTime)) }}
{{ $date2=date('G:i',strtotime($timeSlot->endingTime)) }}
{{ $date2-$date1 }}
My issue is : the difference gives me only hours difference without minutes...
Thanks for your help
Upvotes: 2
Views: 58
Reputation: 163788
You can use Carbon for that. Just tested, works perfectly:
$difference = Carbon::parse($timeSlot->beginningTime)->diff(Carbon::parse($timeSlot->endingTime));
In the view:
{{ $difference->h }}:{{ $difference->i }}
If you cast dates, you don't even need to parse:
$difference = $timeSlot->beginningTime->diff($timeSlot->endingTime);
Upvotes: 2
Reputation: 11636
It's bad practice to have logic inside a view.
Use carbon
and pass data from a controller to the view.
$date1 = Carbon::parse(date('G:i',strtotime($timeSlot->beginningTime)));
$date2 = Carbon::parse(date('G:i',strtotime($timeSlot->endingTime)));
$totalDuration = $date1->diffInSeconds($date2);
Then use gmdate
:
$exactDuration = gmdate('H:i:s', $totalDuration);
// 00:00:21
You can then pass this $exactDuration
to your view and use it there.
Upvotes: 1