-1

I'm using Carbon PHP library.The answer in the duplicate question uses PHP's built in function.

count how many days within a date range are within another date range


Below is the code that I use to find if a date range($userDateStart and $userDateEnd) is with in another date range ($couponStart and$couponEnd`) and it works fine without any error but I don't know how to find the days that overlap/exists that is in this date range?

The library I'm using is http://carbon.nesbot.com/docs/

The expected result should be 4 in this case..Hope you will help me.

$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd  = Carbon::createFromFormat('Y-m-d','2015-06-29');

$couponStart  = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd    = Carbon::createFromFormat('Y-m-d','2015-10-31');

if(($userDateStart >= $couponStart && $userDateEnd <= $couponEnd) ||
    ($couponStart >= $userDateStart && $couponEnd <= $userDateEnd)){
    die("Yes,The date is within this date range");
}
die("No,It is not within this date range");
Community
  • 1
  • 1
user3407278
  • 1,233
  • 5
  • 16
  • 32
  • 4
    The difficulty you have in understanding the answer to the previous question has no bearing whether the question is a duplicate. – Eric Hauenstein Jun 26 '15 at 13:14
  • I'm using Carbon PHP library.Surely there is some other easy way to calculate the days. – user3407278 Jun 26 '15 at 13:16
  • 4
    possible duplicate of [count how many days within a date range are within another date range](http://stackoverflow.com/questions/13227912/count-how-many-days-within-a-date-range-are-within-another-date-range) – rogerdeuce Jun 26 '15 at 13:16
  • @rogerduce He just stated it is not a duplicate. – Twister1002 Jun 26 '15 at 13:20
  • I'm using Carbon PHP library and I don't want to use built in PHP interval class like that answer in duplicate quesiton. – user3407278 Jun 26 '15 at 13:24
  • @JayBlanchard So the difference between a Library he is using vs PHP's built in functions.... I'd say there is no duplication. – Twister1002 Jun 26 '15 at 13:24
  • 1
    http://stackoverflow.com/questions/27245776/using-carbon-to-know-if-a-time-falls-under-two-points-of-time-or-not – Jay Blanchard Jun 26 '15 at 13:26
  • 1
    have you read my question ? @JayBlanchard I want to find how many days are within a date range that are within another date range and it is not as simple as the question you posted. – user3407278 Jun 26 '15 at 13:27
  • 1
    Yes, I have read your question. Please do not ask me things like that when I am trying to be helpful. – Jay Blanchard Jun 26 '15 at 13:29
  • I'm sorry if you find my comment rude. – user3407278 Jun 26 '15 at 13:30
  • no.I there is no need for a DB here. – user3407278 Jun 26 '15 at 13:32
  • 1
    That link I posted explains very clearly how to use `between()` to determine if an instance is between any two instances. It's [right here](http://carbon.nesbot.com/docs/#api-comparison) in the docs. – Jay Blanchard Jun 26 '15 at 13:36

1 Answers1

0

According to the Documentation that was provided, You need to use this:

$dt = Carbon::create(2012, 4, 30, 0);
echo $dt->diffInDays($dt->copy()->addMonth()); // 30
echo $dt->diffInDays($dt->copy()->addWeek()); // 7

So to work with your program I'm thinking you'll need to do this:

$userDateStart = Carbon::createFromFormat('Y-m-d','2015-06-26');
$userDateEnd  = Carbon::createFromFormat('Y-m-d','2015-06-29');

$couponStart  = Carbon::createFromFormat('Y-m-d','2015-06-26');
$couponEnd    = Carbon::createFromFormat('Y-m-d','2015-10-31');

//Determin the highest date from the starts and the minimum dates from the ends
$startBetweenDate = $userDateStart->max($couponStart);
$endBetweenDate = $userDateEnd->min($couponEnd);

//Now find how many days are between
echo $startBetweenDate->diffInDays($endBetweenDate); //Should be 4

Please note: This was not tested as I do not have Carbon's library installed.

Twister1002
  • 559
  • 1
  • 9
  • 26