I am using Carbon to calculate the time between two dates. I'd like to be able to take difference between two dates and determine the time in decimal format to be able to calculate hourly rate. From my testing, the ->diffInHours() call gives me the hours between two dates in whole numbers.
For example
$actual_start_at = Carbon::parse('2017-05-01 13:00:00');
$actual_end_at = Carbon::parse('2017-05-01 15:15:00');
return $actual_end_at->diffInHours($actual_start_at, true);
Returns
2
I would like something that returns to get the 2 hours 15 minutes
2.25
Unfortunately, diffInHours
only take two parameters. Maybe, you can try diffInMinutes
though and then get the value you require from there?
For e.g.
$actual_start_at = Carbon::parse('2017-05-01 13:00:00');
$actual_end_at = Carbon::parse('2017-05-01 15:15:00');
$mins = $actual_end_at->diffInMinutes($actual_start_at, true);
dd($mins/60);
would output
2.25
Also, if you use the diff()
method, it would return a DateInterval
object instead.
$mins = $actual_end_at->diff($actual_start_at, true);
and then dd($mins)
would output:
DateInterval {#913 ▼
+"y": 0
+"m": 0
+"d": 0
+"h": 2
+"i": 15
+"s": 0
+"f": 0.0
+"weekday": 0
+"weekday_behavior": 0
+"first_last_day_of": 0
+"invert": 0
+"days": 0
+"special_type": 0
+"special_amount": 0
+"have_weekday_relative": 0
+"have_special_relative": 0
}