Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david2000's avatar

Calculate hours

I have some hesitations about 3 tables which are type_training , training & payment.

In the table type_training, I have a field named price with 4 amounts: for example:

1 hour 00 = 100 euros

1 hour 30 = 150 euros

2 hour 00 = 200 euros

2 hour 30 = 250 euros

In my page Training , I encode 2 recordings for the same student.

The student Dujardin has booked 3 hours for 300 euros.

In my form Payment, is it possible to retrieve the amount of 300 ?

So, in my Model Payment? I must to calculate the difference between the hour start and the hour end?

I don't know how to do ?

Then, after having retrieved the difference of hours in my example we have 3 hours. How to I sum my 2 recordings in my field Total ? I have tried this?

    $typetraining = Typetraining::find($request->fk_typetraining);
    $data = $request->all(); 
    $data['total'] = $typetraining->price + $request->????;
    Payment::create($data);

In summary:

  1. How to retrieve the difference between hour start & hour end,

  2. How to calculate the amounts via the duration of my training?

For information, here is my architecture.

I thank you for your help and your explanations.

0 likes
3 replies
Ashraam's avatar
Ashraam
Best Answer
Level 41

If hour start and hour end are Carbon instance then you can calculate time like this

$payment->hour_start->diffInMinutes($payment->hour_end);
1 like
david2000's avatar

In my model Training I have this ?

$start = Carbon::parse($request->get('hour_start'));
$end= Carbon::parse($request->get('hour_end'));

$mins = $end->diffInMinutes($start, true);
$hoursTraining = $mins/60;

Then, I calcule the total always in my model Training ?

$total = $typeTraining->price * $hoursTraining; 

I have an error message Undefined variable: typeTraining

Sinnbeck's avatar

Where do you set $typeTraining? Can you show the whole method?

1 like

Please or to participate in this conversation.