Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Smsma's avatar
Level 2

get all dates between s_date and e_date

I try to get all dates between s_date and e_date how can I do it using Carbon I want to get dates like this :

2018-10-10 2018-10-11 2018-10-12 2018-10-13

not the count of days between two dates

 public function dates(Request $request)
    {
        $rules = [
            
            's_date'   => 'required|date|date_format:Y-m-d|after:today',
            'e_date'   => 'required|date|date_format:Y-m-d|after:s_date',

        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails())
            return ApiController::respondWithError(validateRules($validator->errors(), $rules));

       
        $dates = Carbon::parse($request->input($request->s_date))->diffInDays(Carbon::parse($request->input($request->e_date)));

        
        return response()->json($dates);
    
    }
0 likes
10 replies
aurawindsurfing's avatar

Hi,

I would do:

$s_date = Carbon::parse($request->input($request->s_date));
$e_date = Carbon::parse($request->input($request->e_date));

$length = $s_date->diffInDays($e_date);

$dates = [];

for ($i = 0; $i < $length; $i++){ 
   $date = $s_date->addDays($i);
   array_push($dates, $date) 
}

dd($dates);

Hope it helps!

Cronix's avatar

First thing is to remove date as a validation rule. The docs state you should use date or date_format, but not both. https://laravel.com/docs/5.7/validation#rule-date-format

$start = Carbon\Carbon::parse($request->s_date);
$end = Carbon\Carbon::parse($request->e_date);
$dateRange = Carbon\CarbonPeriod::create($start, $end);

$dates = [];
foreach($dateRange as $date) {
    $dates[] = $date->format('Y-m-d');
}

dd($dates);

I try to get all dates between s_date and e_date

If you don't want to include the start and end date in the range (just dates between them), then add ->addDay() to the end of $start, and ->subDay() to the end of $end, like

$start = Carbon\Carbon::parse($request->s_date)->addDay();
$end = Carbon\Carbon::parse($request->e_date)->subDay();

https://carbon.nesbot.com/docs/#api-period

Smsma's avatar
Level 2

Class 'App\Http\Controllers\Api\CarbonPeriod' not found

Cronix's avatar

You didn't add the namespace for it like I did, so it's trying to find CarbonPeriod in the current namespace (where this controller is).

$dateRange = Carbon\CarbonPeriod::create($start, $end);
Cronix's avatar

@Smsma I also forgot to mention that if you want the array of dates to still be carbon objects (that can then be further manipulated) instead of just date strings like 2018-10-12, then instead of

$dates = [];
foreach($dateRange as $date) {
    $dates[] = $date->format('Y-m-d');
}

You could just have

$dates = $dateRange->toArray();

Then you'd just

@foreach($dates as $date)
    {{ $date->format(howYouWant) }}
@endforeach
1 like
Smsma's avatar
Level 2

Class 'Carbon\Carbon\CarbonPeriod' not found

use Carbon\Carbon;

.
.
.



public function dates(Request $request)
    {
        $rules = [
            
            's_date'   => 'required|date|date_format:Y-m-d|after:today',
            'e_date'   => 'required|date|date_format:Y-m-d|after:s_date',

        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails())
            return ApiController::respondWithError(validateRules($validator->errors(), $rules));

        $start = Carbon::parse($request->s_date);
        $end = Carbon::parse($request->e_date);
        $dateRange = Carbon\CarbonPeriod::create($start, $end);
        
        $dates = [];
        foreach($dateRange as $date) {
            $dates[] = $date->format('Y-m-d');
        }
        
        dd($dates);
        
        return response()->json($dates);
    
    }
D9705996's avatar
use Carbon\Carbon;
use Carbon\CarbonPeriod; // add this
...
dateRange = CarbonPeriod::create($start, $end); // change this line
Smsma's avatar
Level 2

Class 'Carbon\CarbonPeriod' not found

Cronix's avatar
Cronix
Best Answer
Level 67

Which version of carbon and/or laravel are you using? I tested it in tinker using Laravel 5.5 before posting, which uses Carbon v1.32.0.

from cli

> composer show | grep carbon
nesbot/carbon                         1.32.0  A simple API extension for DateTime.

Please or to participate in this conversation.