Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get difference between two intervals of time in Carbon

Tags:

php

laravel

I have two intervals of times , let's say

  • Interval X: [01-11-2019 08:00, 01-11-2019 14:00]
  • Interval Y: [01-11-2019 12:00, 01-11-2019 17:00]

I need to get the intersect between these intervals so the answer should be 2 hours because the interval Y intersect with interval X in 2 hours only, so how to do that in Carbon ? Is there any library or function to do that ? I've searched but no useful results. Thanks

Please note i mean difference between two intervals not just startDate and endDate

like image 820
Mohamed Gamal Avatar asked Nov 07 '19 13:11

Mohamed Gamal


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 does Carbon add time?

You can add hours on current date using carbon in laravel 6, laravel 7, laravel 8 and laravel 9 version. If you need to add hour or more hours in date then you can use carbon in laravel. carbon provide addHour() and addHours() method to add hours on carbon date object.

How do you calculate the time interval between two variables?

Note that %H, %M and %S are the representatives of Hours, Minutes and, Seconds. After the two time strings are stored with their time format, the time interval between the two is calculated by simply subtracting the two variables.

How to calculate the difference between two times on the same day?

If you are just interested in the difference and know that both times are on the same day then construct a datetimefor each with the day set to today and subtract the start from the stop time to get the interval (timedelta). Share Improve this answer Follow answered Jun 22 '10 at 20:43

How to calculate time difference between two dates in Excel?

Subtracting Two Dates to Calculate Time Difference in Excel Between Two Dates The simplest way to calculate the time difference between two days is to use the generic subtraction formula. ❶ Insert the following formula in cell E5.

How to calculate the time difference between two time strings in Python?

After the two time strings are stored with their time format, the time interval between the two is calculated by simply subtracting the two variables. Use time.sleep () to Calculate the Time Difference Between Two Time Strings in Python


2 Answers

An interval represents an amount of time passed from timeA to timeB regardless of a start time or an end time. I guess you mean a period (date range).

You can calculate the overlap (intersection) between two date ranges using CarbonPeriod class and simple function.

I would like to suggest the following implementation:

<?php
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;

function calculatePeriodsOverlap(CarbonPeriod $periodA, CarbonPeriod $periodB): CarbonInterval
{
    if (!$periodA->overlaps($periodB)) {
        return new CarbonInterval(0);
    }

    $firstEndDate = min($periodA->calculateEnd(), $periodB->calculateEnd());
    $latestStartDate = max($periodA->getStartDate(), $periodB->getStartDate());

    return CarbonInterval::make($firstEndDate->diff($latestStartDate));
}


$periodX = new CarbonPeriod('01-11-2019 08:00', '01-11-2019 14:00');
$periodY = new CarbonPeriod('01-11-2019 12:00', '01-11-2019 17:00');

calculatePeriodsOverlap($periodX, $periodY)->forHumans(); // returns "2 hours"
like image 200
d521bb85 Avatar answered Oct 25 '22 08:10

d521bb85


You don't need a library how carbon. Use simply DateTime.

$iX0 = date_create('01-11-2019 08:00');  //Start interval X
$iX1 = date_create('01-11-2019 14:00');  //End interval X
$iY0 = date_create('01-11-2019 12:00');  //Start interval Y
$iY1 = date_create('01-11-2019 17:00');  //End interval Y

$i0 = MAX($iX0,$iY0);
$i1 = Min($iX1,$iY1);
if($i1 >= $i0){
  $diff = $i0->diff($i1);
  echo $diff->h." Hours";  //full Hours
}
else {
  echo 'no itersect';
}

Output:

2 Hours

Note: This example only calculates full hours without minutes and seconds.

Try it yourself: https://3v4l.org/uS1Fh

like image 2
jspit Avatar answered Oct 25 '22 06:10

jspit