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
Closed

range of dates #132

fariss opened this issue Jun 19, 2014 · 12 comments

Comments

@fariss
Copy link

@fariss fariss commented Jun 19, 2014

How to create a range of dates ?

@Anahkiasen
Copy link
Contributor

@Anahkiasen Anahkiasen commented Jun 19, 2014

What do you mean?

Loading

@fariss
Copy link
Author

@fariss fariss commented Jun 19, 2014

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

Loading

@Anahkiasen
Copy link
Contributor

@Anahkiasen Anahkiasen commented Jun 19, 2014

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

Loading

@kylekatarnls
Copy link
Collaborator

@kylekatarnls 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;

Loading

@briannesbitt
Copy link
Owner

@briannesbitt 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();
}

Loading

@fariss
Copy link
Author

@fariss 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);

Loading

@kylekatarnls
Copy link
Collaborator

@kylekatarnls 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.

Loading

@briannesbitt
Copy link
Owner

@briannesbitt 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();
}

Loading

@briannesbitt
Copy link
Owner

@briannesbitt 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();
}

Loading

@fariss
Copy link
Author

@fariss fariss commented Jun 19, 2014

Nice 👍

thank you @briannesbitt

Loading

@reinink
Copy link

@reinink 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) {
   // ...
}

Loading

kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Apr 14, 2018
@kylekatarnls
Copy link
Collaborator

@kylekatarnls kylekatarnls commented Apr 14, 2018

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants