Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon difference between two dates to get a decimal for hourly rate calculations

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
like image 762
jdcarg Avatar asked May 22 '17 04:05

jdcarg


People also ask

How do you find the difference between two dates in Carbon?

You can only use the diffInDays() function on a Carbon instance. You can create a new one by parsing the end date you're receiving. $end = Carbon::parse($request->input('end_date'));

How do you find the difference between two times in laravel Carbon?

Help of Carbon class we can simply calculate minutes difference between two dates. you can simply customize code also. In Following example you can see, we have a two dates, first $to variable and second one $from variable. Carbon class diffInDays function using you can get difference between two dates.

How can I find the difference between two dates in laravel 8?

function dateDiffInDays($date1, $date2) { $diff = strtotime($date2) - strtotime($date1); return abs(round($diff / 86400)); } // Start date $date1 = "2021-03-01 04:03:00"; // End date $date2 = date('Y-m-d'); $dateDiff = dateDiffInDays($date1, $date2);


2 Answers

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
}
like image 96
Mozammil Avatar answered Oct 23 '22 14:10

Mozammil


Here's a more complete solution using CarbonPeriod.

Carbon Calculate hours in an interval from a period

<?php

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Carbon\CarbonInterval;

class PeriodInterval {
    public function hoursIn() {
        /**
        * Time Periods
        */
        $periods = [
            'day' => [
                '08:00:00',
                '18:00:00'
            ],
            'night' => [
                '18:00:00',
                '08:00:00'
            ],
            'lunch' => [
                '11:00:00',
                '14:00:00'
            ]
        ];

        /**
        * Time Intervals
        */
        $intervals = [
            'parking' => [
                'John Doe' => [
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 07:00:00'),
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 09:00:00')
                ],
                'Jane Doe' => [
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 08:00:00'),
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 09:00:00')
                ],
                'John Hope' => [
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 11:00:00'),
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 13:00:00')
                ],
                'Jane Hope' => [
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 18:00:00'),
                    Carbon::createFromFormat('Y-m-d H:i:s', '2018-12-10 19:00:00')
                ]
            ]
        ];

        /**
        * Hours for Interval in Period
        */
        function hoursForIntervalInPeriod(array $period, array $interval) {
            $Period = new CarbonPeriod(array_shift($interval)->toDateTimeString(), '1 hour', array_pop($interval)->toDateTimeString());

            return $Period->filter(function($date) use ($period) {
                $period_starts = Carbon::createFromFormat('Y-m-d H:i:s', $date->format('Y-m-d') . ' ' . array_shift($period));
                $period_ends = Carbon::createFromFormat('Y-m-d H:i:s', $date->format('Y-m-d') . ' ' . array_pop($period));

                // Normal Period
                if ($period_starts < $period_ends) {
                    return ($date >= $period_starts && $date < $period_ends);
                }

                // Inverted Period (Overnight)
                if ($period_starts > $period_ends) {
                    return ($date >= $period_starts || $date < $period_ends);
                }

                return false;
            })->count();
        }

        /**
        * Map Everything
        */
        $hours = array_map(function($interval, $interval_key) use ($periods) {
            return array_map(function ($period, $period_key) use ($interval, $interval_key) {
                return [ $interval_key . ucfirst($period_key) => array_map(function($interval_item, $interval_item_key) use ($period) {
                    /**
                    * Calculate Total Hours
                    */
                    $total_hours = hoursForIntervalInPeriod($period, $interval_item);

                    return [ $interval_item_key => $total_hours ];
                }, $interval, array_keys($interval)) ];
            }, $periods, array_keys($periods));
        }, $intervals, array_keys($intervals));

        return $hours;
    }
}
like image 22
Nikola Hristov Avatar answered Oct 23 '22 12:10

Nikola Hristov