Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

range of dates #132

Closed
fariss opened this Issue Jun 19, 2014 · 12 comments

Comments

Projects
None yet
5 participants
@fariss
Copy link

fariss commented Jun 19, 2014

How to create a range of dates ?

@Anahkiasen

This comment has been minimized.

Copy link
Contributor

Anahkiasen commented Jun 19, 2014

What do you mean?

@fariss

This comment has been minimized.

Copy link
Author

fariss commented Jun 19, 2014

i want for exemple, create a range of dates between today and today + 20 days

@Anahkiasen

This comment has been minimized.

Copy link
Contributor

Anahkiasen commented Jun 19, 2014

You mean like if you do Carbon::parse('+20 days')->diff() ? Like a DateInterval object ?

@kylekatarnls

This comment has been minimized.

Copy link
Collaborator

kylekatarnls commented Jun 19, 2014

A very primitive example:

class DateRange {
    public Carbon $start;
    public Carbon $end;
    public function __construct(Carbon $start, Carbon $end) {
        $this->start = $stard;
        $this->end= $end;
    }
}
$todayToPlus20 = new DateRange(Carbon::now(), Carbon::now()->addDays(20));
echo $todayToPlus20->start;
echo $todayToPlus20->end;
@briannesbitt

This comment has been minimized.

Copy link
Owner

briannesbitt commented Jun 19, 2014

If its an array of 20 dates you are after to just add them to an array ??

$start = Carbon::now();  // use your start here

for ($i = 0 ; $i < 20 ; $i++) {
   $dates[] = $start->copy();
   $start->addDay();
}
@fariss

This comment has been minimized.

Copy link
Author

fariss commented Jun 19, 2014

what i want is an array of dates between $startDate and $endDate:

$startDate = Carbon::now();
$endDate = '2014-07-30';
// result
array($startDate, $startDate+1day, $startDate+2day,..., $endDate);
@kylekatarnls

This comment has been minimized.

Copy link
Collaborator

kylekatarnls commented Jun 19, 2014

briannesbitt's answer works in your case, replace juste $i < 20 with something like $start->format('Y-m-d') != $endDate.

@briannesbitt

This comment has been minimized.

Copy link
Owner

briannesbitt commented Jun 19, 2014

$start = Carbon::today();
$end = new Carbon('2014-07-30');
$end->startOfDay();
$dates = array();

while ($start->lte($end)) {
   $dates[] = $start->copy();
   $start->addDay();
}
@briannesbitt

This comment has been minimized.

Copy link
Owner

briannesbitt commented Jun 19, 2014

... and if you wanted it shorter (but harder to read IMO)

$start = Carbon::yesterday();
$end = new Carbon('2014-07-30');
$end->startOfDay();
$dates = array();

while ($start->lte($end)) {
   $dates[] = $start->addDay()->copy();
}
@fariss

This comment has been minimized.

Copy link
Author

fariss commented Jun 19, 2014

Nice 👍

thank you @briannesbitt

@reinink

This comment has been minimized.

Copy link

reinink commented Nov 25, 2017

This can also be done like this:

new DatePeriod($startDate, new DateInterval('P1D'), $endDate)

Just keep in mind that DatePeriod is an iterator, so if you want an actual array:

iterator_to_array(new DatePeriod($startDate, new DateInterval('P1D'), $endDate))

In you're using Laravel, you could always create a Carbon macro:

Carbon::macro('range', function ($start, $end) {
    return new Collection(new DatePeriod($start, new DateInterval('P1D'), $end));
});

Now you can do this:

foreach (Carbon::range($start, $end) as $date) {
   // ...
}

kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Apr 14, 2018

@kylekatarnls

This comment has been minimized.

Copy link
Collaborator

kylekatarnls commented Apr 14, 2018

Will be added to the documentation:
gh-pages...kylekatarnls:gh-pages-1.26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.