0

I have two date fields Start and End

         <input type="date" name="start>
         <input type="date" name="end>

Now i want to get number of days between the selected range and all the dates which comes in this range.

Rob
  • 14,746
  • 28
  • 47
  • 65
Muhammad Kazim
  • 611
  • 2
  • 11
  • 26
  • 1
    Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – felipsmartins Jan 13 '18 at 22:11
  • 1
    There is so many questions regarding this question. – felipsmartins Jan 13 '18 at 22:12

2 Answers2

3

You can use Carbon's diffInDays() method:

Carbon::parse(request('start'))->diffInDays(Carbon::parse(request('end')));

To list all the dates between these two dates:

$period = new DatePeriod(Carbon::parse(request('start')), CarbonInterval::day(), Carbon::parse(request('end')));
foreach ($period as $date) {
    echo $date;
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • great it shows the number of days and how i get the dates which comes in this range ? – Muhammad Kazim Jan 13 '18 at 22:16
  • This does not include the start date. I want to get the count in days from start to end like date 16-25 I think return should be 10 days. But returning 9 days. should include 16 and 25 dates also. How I will do that? – Abdullah Iftikhar Nov 16 '21 at 08:12
1

Use CarbonPeriod to get day beetween two date

$period = \Carbon\CarbonPeriod::create(request('start'), request('end'));
foreach ($period as $key => $date) {
   dump($date->format('Y-m-d'));
}

With carbon period you can get both start and end date.

ahmat arnal
  • 41
  • 1
  • 6