Mar55's avatar
Level 1

How can i get all the dates between two dates in Laravel ?

I tried this method with Carbon, but it redirected me to an error (Carbon \ Exceptions \ InvalidFormatException Trailing data):

namespace App\Http\Controllers;
  
use Carbon\Carbon;
use Carbon\CarbonPeriod;
  
class SampleController extends Controller
{
    public function index()
    {
        $startDate = Carbon::createFromFormat('Y-m-d', '2021-10-01');
        $endDate = Carbon::createFromFormat('Y-m-d', '2021-10-05');
  
        $dateRange = CarbonPeriod::create($startDate, $endDate);
   
        dd($dateRange->toArray());
    }
}

Do you know any other way ?

0 likes
14 replies
Emokores's avatar

Are you trying to get the data from the database? Do you have start_date and end_date fields in your table? Or are you trying to query between a date field and current date?

tykus's avatar

Are you accepting the start and end dates data from the Request - something must be dynamic if that Exception is thrown?

surajjadhav's avatar

You can use iterator_to_array function to create array of CarbonPeriod object. With that your solution will look like this.

$startDate = Carbon::createFromFormat('Y-m-d', '2021-10-01');
$endDate = Carbon::createFromFormat('Y-m-d', '2021-10-05');

$dateRange = CarbonPeriod::create($startDate, $endDate);

$dates = array_map(fn ($date) => $date->format('Y-m-d'), iterator_to_array($dateRange));
tykus's avatar

@surajjadhav that is what toArray method on the CarbonPeriod instance does.

The OP's problem come from createFromFormat however, which your implementation does not address.

Probably, the OP is receiving the start and end dates from the Request in a different format; and needs to adapt for that!

1 like
surajjadhav's avatar

@tykus I see, then I misunderstood the question in the first place. :stuck_out_tongue:

jlrdw's avatar

@mar55 just write an sql query and use between, i.e.

// top part of query
$sql .= "where (`transactions`.`TransactionDate` Between :bdate and  :edate) ";
//rest of query

Bind

$params = ['bdate' => $bdate, 'edate' => $edate];

Just example. But what are you attempting to do just get a list of days? You will need to foreach if that's the case.

tykus's avatar

@jlrdw the OP code is not querying the database - why do you suggest SQL as a solution for an array of dates?

jlrdw's avatar

@tykus I also mis-understood question. OP will need a foreach loop.

jlrdw's avatar

@tykus then if no loop this should work:

        $dateRange = CarbonPeriod::create('2021-10-01', '2021-10-05');
        $dates = $dateRange->toArray();
        dd($dates);

Edit

Or for nicer list:

        foreach ($daterange as $date) {
            echo $date->format('Y-m-d') . "<br>";
        }

Gives:

2021-10-01
2021-10-02
2021-10-03
2021-10-04
2021-10-05

Just example.

1 like
tykus's avatar

@jlrdw the OP's problem is the actual date string being used to create either the start or end Carbon instance(s) as I mentioned previously above.. The CarbonPeriod instance is not the problem; and a loop does nothing to solve this...

Mar55's avatar
Level 1

@jlrdw Thanks for you help that's what i was looking for, would you also know how i can set it to the format ("Y-m") instead of ("Y-m-d") ? Edit: I found the solution, i just had to change $dateRange to this:

$dateRange = CarbonPeriod::create('2021-10-01', '1 month',  '2021-10-05');
tykus's avatar

@Mar55 what are you doing? Your date range will have exactly one date - why are you looking to create a Period with 1 month interval when the dates are days apart?

And none of this relates to the InvalidFormatException trailing data error you mentioned in the OP.

Snapey's avatar

The code you posted works fine.

Do you want to instead post the actual code?

Please or to participate in this conversation.