php - Incrementing dates with Carbon -
i'm trying create array of blackout dates reservation system in laravel 4. there 1 test row in db start_date of 2016-01-24 , end_date of 2016-01-29.
this code pulls row , loops through dates using carbon increment 1 day & add array:
$reserved = reservation::where('property_id', $property->id)->get(); $blackoutdays = []; foreach($reserved $r) { $start = new \carbon\carbon($r->start_date); $end = new \carbon\carbon($r->end_date); $days = $start->diff($end)->days; for($i = 0; $i <= $days; $i++) { $date = ''; $date = $start->adddays($i); $blackoutdays[] = $date->format('y-m-j'); } } what i'm trying in $blackoutdays is:
["2016-01-24", "2016-01-25", "2016-01-26", "2016-01-27", "2016-01-28", "2016-01-29"]
but i'm getting this:
["2016-01-24", "2016-01-25", "2016-01-27", "2016-01-30", "2016-02-3", "2016-02-8"]
does know why happening / how fix it? there better way of doing this?
you increment $i every run of loop. adds 1 in first run, 2 days in second, 3 days in third , on.
therefore, want replace
$date = $start->adddays($i); with
$date = $start->adddays(1); where fell pit idea days added $start date object on every call, not case, object not "immutable".
Comments
Post a Comment